使用filter解决request.getParameter的中文乱码问题

注意:一般一个站点的所有页面的编码,包括数据库编码都要保持一致,下面默认的编码都是UTF-8

----------------------------------例1:直接提交到jsp页面----------------------------------

input_info.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    <form action="display_info.jsp" method="post">

        <!-- 输入中文来测试 -->

        请输入要显示的内容:<input type="text" name="info">

        <input type="submit" value="显示">

    </form>

</body>

</html>

display_info.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

    <!--

        注意这里并没有调用request.setCharacterEncoding(),

        你把CharacterEncodingFilter关闭了试试,看看结果有什么

        不一样

    -->

    <%

        String str = (String) request.getParameter("info");

    %>

    <h1><%=str%></h1>

</body>

</html>

CharacterEncodingFilter.java

  charset参数是从web.xml中配置好的,这里读取进来

 1 public final class CharacterEncodingFilter implements Filter {

 2     private String encoding = null; 

 3     private boolean enable = false; 

 4     public void destroy() { 

 5         this.encoding = null; 

 6     } 

 7     /**

 8      * 完成request.setCharacterEncoding(encoding);

 9      */

10     public void doFilter(ServletRequest request, ServletResponse response, 

11                            FilterChain chain) throws IOException, ServletException { 

12         if (this.enable) {

13             String encoding = this.selectEncoding(request); 

14             if (encoding != null && !encoding.equals("")) { 

15                 request.setCharacterEncoding(encoding);

16             }

17         }

18         // Pass control on to the next filter 

19         chain.doFilter(request, response);

20         // After other filters are done:

21         if (this.enable) {

22             String encoding = this.selectEncoding(request); 

23             if (encoding != null && !encoding.equals("")) {

24                 // response.setCharacterEncoding(encoding); // 实验证明这货没起什么作用,所以,不要也行

25             }

26         }

27      } 

28      /**

29       * 读取encoding参数以及enable参数,enable参数与encoding的功能无关,只是用来启用或者停用这个filter的

30       */

31     public void init(FilterConfig filterConfig) throws ServletException {

32         this.encoding = filterConfig.getInitParameter("encoding");

33         if (!Charset.isSupported(encoding)) {

34             encoding = null; // 如果不支持的话,只能选择默认的encoding了,这个filter就不起作用

35         }

36         // 读取enable参数

37         String enableString = filterConfig.getInitParameter("enable");

38         if (enableString.equalsIgnoreCase("true")) {

39             this.enable = true;

40         } else {

41             this.enable = false;

42         }

43     }

44 }

web.xml

 1     <filter> 

 2         <filter-name>charset-encoding</filter-name> 

 3         <filter-class>org.foo.filterdemo.CharacterEncodingFilter</filter-class> 

 4         <init-param> 

 5             <param-name>encoding</param-name> 

 6             <param-value>UTF-8</param-value> 

 7         </init-param> 

 8         <init-param> 

 9             <param-name>enable</param-name> 

10             <param-value>true</param-value> 

11         </init-param> 

12     </filter>

13     <filter-mapping>

14         <filter-name>charset-encoding</filter-name>

15         <url-pattern>/*</url-pattern>

16     </filter-mapping>

--------例2:提交到Servlet,Serlvet再设置为request的attribute,再提交到jsp页面--------

AttributeSetterServlet.java

 1 public class AttributeSetterServlet extends HttpServlet {

 2     /**

 3      * Generated serialVersionUID

 4      */

 5     private static final long serialVersionUID = 8458482445490259121L;

 6 

 7     @Override

 8     public void doGet(HttpServletRequest req, HttpServletResponse resp)

 9             throws ServletException, IOException {

10         // 在这里也没有调用request.setCharacterEncoding(),

11         // 你把CharacterEncodingFilter关闭了试试,看看结果有什么

12         // 不一样

13         String attr = req.getParameter("attr");

14         if (attr != null) {

15             // System.out.println(this + ", " + attr); // @Debug

16             attr += " -- 这是一个小尾巴";

17             req.setAttribute("attr", attr);

18             req.getRequestDispatcher("display_attribute.jsp").forward(req, resp);

19         }

20     }

21     @Override

22     public void doPost(HttpServletRequest req, HttpServletResponse resp)

23             throws ServletException, IOException {

24         this.doGet(req, resp);

25     }

26 }

input_attribute.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 2 <html>

 3 <head>

 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 5 <title>Insert title here</title>

 6 </head>

 7 <body>

 8     <form action="AttributeSetterServlet" method="post">

 9         <!-- 输入中文来测试 -->

10         请输入要设置的attribute:<input type="text" name="attr">

11         <input type="submit" value="显示">

12     </form>

13 </body>

14 </html>

display_attribute.jsp

<%@ page language="java" contentType="text/html"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Insert title here</title>

</head>

<body>

    <h1>设置的attribute是:<%=request.getAttribute("attr")%></h1>

</body>

</html>

web.xml中添加如下代码:

 1     <servlet>

 2         <servlet-name>attribute-setter-servlet</servlet-name>

 3         <servlet-class>

 4             org.foo.servletdemo.AttributeSetterServlet

 5         </servlet-class>

 6     </servlet>

 7     <servlet-mapping>

 8         <servlet-name>attribute-setter-servlet</servlet-name>

 9         <url-pattern>/AttributeSetterServlet</url-pattern>

10     </servlet-mapping>

 

你可能感兴趣的:(parameter)