首页 > 代码库 > 如何配置Filter过滤器处理JSP中文乱码

如何配置Filter过滤器处理JSP中文乱码

参考Tomcat服务器目录webapps的examples示例

简单配置步骤:
1.在项目web.xml文件添加过滤器标记<filter>和<filter-mapping>;
2.实现过滤器代码;
3.对Tomcat服务器conf目录里的Server.xml文件配置URIEncoding;
4.前台页面设置contentType的charset值与web.xml里设置的值一致。

详细配置步骤:
1.配置web.xm文件,在web-app标签内添加过滤器的配置<filter>和<filter-mapping>

[html] view plaincopy

<!--中文乱码处理过滤-->

  1. <filter>
  2. <filter-name>EncodingFilter</filter-name><!--过滤器名称,自己起 -->
  3. <filter-class>com.filters.SetCharacterEncodingFilter</filter-class><!--过滤器类名称,包括包名一致、写全 -->
  4. <init-param><!--初始化参数,要指定的字符集编码 -->
  5. <param-name>encoding</param-name><!--参数名称 -->
  6. <param-value>GBK</param-value><!--参数值 -->
  7. </init-param>
  8. <init-param><!--初始化参数,指定是否忽略大小写 -->
  9. <param-name>ignore</param-name>
  10. <param-value>true</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>EncodingFilter</filter-name>
  15. <url-pattern>/*</url-pattern> <!--“/*”表示项目下所有资源 -->
  16. </filter-mapping>
  17. <!--中文乱码处理过滤 -->

2.实现过滤器代码,创建一个SetCharacterEncodingFilter

[java] view plaincopy

  1. package com.filters;  
  2. import java.io.IOException;  
  3. import javax.servlet.Filter;  
  4. import javax.servlet.FilterChain;  
  5. import javax.servlet.FilterConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.ServletRequest;  
  8. import javax.servlet.ServletResponse;  
  9. /**
  10. * 自定义字符处理过滤器
  11. * 参考自Tomcat目录webapps\examples\WEB-INF\classes\filters
  12. * @author coderlu
  13. * @since 2012-12-10
  14. */
  15. public class SetCharacterEncodingFilter implements Filter {  
  16. protected FilterConfig filterConfig = null; //初始化配置
  17. protected String encoding = null;           //接收字符编码
  18. protected boolean ignore = true;            //是否忽略大小写
  19. /* 析构函数
  20.      * @see javax.servlet.Filter#destroy()
  21.      */
  22. @Override
  23. public void destroy() {  
  24. this.encoding = null;  
  25. this.filterConfig = null;  
  26.     }  
  27. /* 执行过滤操作
  28.      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  29.      */
  30. @Override
  31. public void doFilter(ServletRequest request, ServletResponse response,  
  32.             FilterChain chain) throws IOException, ServletException {  
  33. if (ignore || (request.getCharacterEncoding() == null)) {  
  34.             String encoding = selectEncoding(request);  //如果为空先从web.xml中得到
  35. if (encoding != null){  
  36.                 request.setCharacterEncoding(encoding); //设置字符集编码
  37.             }  
  38.         }  
  39.         chain.doFilter(request, response);  
  40.     }  
  41. /* 初始化过滤器
  42.      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  43.      */
  44. @Override
  45. public void init(FilterConfig filterConfig) throws ServletException {  
  46. this.filterConfig = filterConfig;     
  47. this.encoding = filterConfig.getInitParameter("encoding");  //从web.xml文件中读取encoding的值
  48.         String value = http://www.mamicode.com/filterConfig.getInitParameter("ignore");     //从web.xml文件中读取ignore的值
  49. //以下三种情况均为忽略大小写
  50. if (value =http://www.mamicode.com/= null)
  51. this.ignore = true;  
  52. else if (value.equalsIgnoreCase("true"))  
  53. this.ignore = true;  
  54. else if (value.equalsIgnoreCase("yes"))  
  55. this.ignore = true;  
  56. else
  57. this.ignore = false;  
  58.     }  
  59. /**
  60.      * 得到字符编码
  61.      * @param request
  62.      * @return
  63.      */
  64. protected String selectEncoding(ServletRequest request) {  
  65. return (this.encoding);  
  66.     }  

3.修改Tomcat服务器conf\Server.xml文件,加入一个URIEncoding="GBK" 

[html] view plaincopy

  1. <!--URIEncoding 配置服务器反应编码集-->
  2. <Connector port="8080" protocol="HTTP/1.1"
  3. connectionTimeout="20000"
  4. redirectPort="8443"
  5. URIEncoding="GBK"/>

4.设置前台页面字符编码集

[html] view plaincopy

  1. <%@ page contentType="text/html;charset=GBK" %>

//此处的charset的值要和web.xml里的   <param-value></param-value>值一样

=================================完=====================================
参考自:
Tomcat服务器目录webapps的examples示例
jsp中文乱码的Filter过滤器配置:http://www.cnblogs.com/zhuboxingzbx/articles/1208118.html
JSP中文乱码,用过滤器方式解决:http://www.zhouwenze.com/archives/20110729-411.html
JSP中文乱码过滤器实例:http://blog.csdn.net/wkupaochuan/article/details/7461208

http://blog.csdn.net/lutinghuan/article/details/8277422
========================================================================