web.xml配置详解之filter

时间:2022-04-14 13:37:45
  • 定义
[html] view plain copy
  1. <pre class="html" name="code"><filter>  
  2. <filter-name>encodingfilter</filter-name>  
  3. <filter-class>com.my.app.EncodingFilter</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>encodingfilter</filter-name>  
  11. <url-pattern>/*</url-pattern>  
  12. </filter-mapping></pre><br>  
  13. <pre></pre>  
  14. <pre></pre>  
  • 作用

用于指定WEB容器的过滤器,在请求和响应对象在Servlet处理之前和之后,可以通过此过滤器对这两个对象进行处理。

  • 备注

filter-class 中指定的过滤器类须继承 javax.servlet.Filter 具有须有以下三种方法

init(FilterConfig filterConfig):初始化;一般情况下时读取配置文件中的init-param参数值 如 filterConfig.getInitParameter("encoding")

doFilter(...):用于对request,response进行处理,并能过chain.doFilter(...) 交过下一个控制器

destroy():资源销毁

  • 示例:如编码过滤器

web.xml配置

[html] view plain copy
  1. <filter>  
  2. <filter-name>encodingfilter</filter-name>  
  3. <filter-class>com.my.app.EncodingFilter</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>encodingfilter</filter-name>  
  11. <url-pattern>/*</url-pattern>  
  12. </filter-mapping>  


Java 代码

[java] view plain copy
  1. /** 
  2.  *  
  3.  */  
  4. package com.myapp;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import javax.servlet.Filter;  
  9. import javax.servlet.FilterChain;  
  10. import javax.servlet.FilterConfig;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.ServletRequest;  
  13. import javax.servlet.ServletResponse;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16.   
  17. /** 
  18.  * @author louisliao 
  19.  * 
  20.  */  
  21. public class EncodingFilter implements Filter {  
  22.   
  23.     /** 
  24.      * 配置中默认的字符编码 
  25.      */  
  26.     protected String encoding = null;    
  27.     protected FilterConfig filterConfig;  
  28.     /** 
  29.      * 当没有指定默认编码时是否允许跳过过滤 
  30.      */  
  31.     protected boolean ignore = true;   
  32.     /** 
  33.      *  
  34.      */  
  35.     public EncodingFilter() {  
  36.         // TODO Auto-generated constructor stub  
  37.     }  
  38.   
  39.     /* (non-Javadoc) 
  40.      * @see javax.servlet.Filter#destroy() 
  41.      */  
  42.     public void destroy() {  
  43.         // TODO Auto-generated method stub  
  44.         this.encoding=null;  
  45.         this.filterConfig=null;  
  46.     }  
  47.       
  48.     /* (non-Javadoc) 
  49.      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 
  50.      */  
  51.     public void doFilter(ServletRequest request, ServletResponse response,  
  52.             FilterChain chain) throws IOException, ServletException {  
  53.         // TODO Auto-generated method stub  
  54.         HttpServletRequest hRequest=(HttpServletRequest)request;  
  55.         HttpServletResponse hResponse=(HttpServletResponse)response;  
  56.         //Conditionally select and set the character encoding to be used    
  57.         if(ignore || hRequest.getCharacterEncoding()==null){  
  58.             String coding=selectEncoding(hRequest);  
  59.             if(coding!=null){  
  60.                 hRequest.setCharacterEncoding(coding);  
  61.                 hResponse.setCharacterEncoding(coding);  
  62.             }  
  63.         }  
  64.         //将控制器传向下一个filter  
  65.         chain.doFilter(hRequest, hResponse);  
  66.   
  67.     }  
  68.   
  69.     /* (non-Javadoc) 
  70.      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 
  71.      */  
  72.     public void init(FilterConfig filterConfig) throws ServletException {  
  73.         // TODO Auto-generated method stub  
  74.         this.filterConfig=filterConfig;  
  75.         this.encoding=filterConfig.getInitParameter("encoding");  
  76.         System.out.println(this.encoding);  
  77.         String value = filterConfig.getInitParameter("ignore");  
  78.         if (value == null) {  
  79.             this.ignore = true;  
  80.         } else if (value.equalsIgnoreCase("true")) {  
  81.             this.ignore = true;  
  82.         } else if (value.equalsIgnoreCase("yes")) {  
  83.             this.ignore = true;  
  84.         } else {  
  85.             this.ignore = false;  
  86.         }  
  87.     }  
  88.     protected String selectEncoding(ServletRequest request) {  
  89.         return (this.encoding);  
  90.     }  
  91.   
  92. }  
  93. init方法是在WEB应用启动就会调用,doFilter则是在访问filter-mapping映射到的url时会调用。