tomcat中的一个过滤器例子

感谢:http://www.iteye.com/wiki/Struts/578-Struts原理与实践(5) 和Google

将所有的request的字符集也设置为UTF-8。虽然,我们可以在每个文件中加入这样的句子:request.setCharacterEncoding("UTF-8");来解决,但这样显得很麻烦。一种更简单的解决方法是使用filter。具体步骤如下: 

在mystruts\WEB-INF\classes目录下再新建一个名为filters的目录,新建一个名为:SetCharacterEncodingFilter的类,并保存在该目录下。其实,这个类并不要您亲自来写,可以借用tomcat中的例子。现将该例子的程序节选如下: 

  1. package 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. import javax.servlet.UnavailableException;   
  10.  
  11. public class SetCharacterEncodingFilter implements Filter {   
  12.   
  13.   
  14.  //可以理解为默认编码,在web.xml中配置后读取存入
  15.     protected String encoding = null;   
  16.   

  17.     protected FilterConfig filterConfig = null;   

  18.     protected boolean ignore = true;   
  19.   
  20.     public void destroy() {   
  21.   
  22.         this.encoding = null;   
  23.         this.filterConfig = null;   
  24.   
  25.     }   
  26.   
  27.   
  28.     public void doFilter(ServletRequest request, ServletResponse response,   
  29.                          FilterChain chain)   
  30.         throws IOException, ServletException {   
  31.   
  32.         // Conditionally select and set the character encoding to be used 
  33. //如果被忽略或者请求未设置编码,为了防止乱码,进行编码过滤  
  34.         if (ignore || (request.getCharacterEncoding() == null)) {   
  35. //设置请求编码为web.xml中配置的编码类型
  36.             String encoding = selectEncoding(request);   
  37.             if (encoding != null)   
  38.                 request.setCharacterEncoding(encoding);   
  39.         }   
  40.   
  41.         // Pass control on to the next filter 
  42. //由FilterChain进行过滤处理,而不是该Filter  
  43.         chain.doFilter(request, response);   
  44.   
  45.     }   
  46.   
  47.  //主要从配置文件中读取配置项并进行赋值
  48.     public void init(FilterConfig filterConfig) throws ServletException {   
  49.   
  50.         this.filterConfig = filterConfig;   
  51.   //从web.xml读取配置的编码,如gbk,utf-8等,赋值给encoding
  52.         this.encoding = filterConfig.getInitParameter("encoding");  
  53.   //从web.xml读取是否忽略
  54.         String value = filterConfig.getInitParameter("ignore");  
  55.         if (value == null)  
  56.             this.ignore = true;  
  57.         else if (value.equalsIgnoreCase("true"))  
  58.             this.ignore = true;  
  59.         else if (value.equalsIgnoreCase("yes"))   
  60.             this.ignore = true;   
  61.         else  
  62.             this.ignore = false;   
  63.   
  64.     }   
  1.  protected String selectEncoding(ServletRequest request) {   
  2.   
  3.         return (this.encoding);   
  4.   
  5.     }   
  6.   
  1. <filter>  
  2.     <filter-name>Set Character Encoding</filter-name>  
  3.     <filter-class>filters.SetCharacterEncodingFilter</filter-class>  
  4.     <init-param>  
  5.       <param-name>encoding</param-name>  
  6.       <param-value>UTF-8</param-value>  
  7.     </init-param>  
  8.   </filter>  
  9.   <filter-mapping>  
  10.     <filter-name>Set Character Encoding</filter-name>  
  11.     <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值