标签:
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
Filters perform filtering in the doFilter method. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext which it can use, for example, to load resources needed for filtering tasks.
Filters are configured in the deployment descriptor of a web application.
Examples that have been identified for this design are:
The init method called by the web container to indicate to a filter that it is being placed into service.
The servlet container calls the init method exactly once after instantiating the filter. The init method must complete successfully before the filter is asked to do any filtering work.
The web container cannot place the filter into service if the init method either
The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.
A typical implementation of this method would follow the following pattern:
The destroy method called by the web container to indicate to a filter that it is being taken out of service.
This method is only called once all threads within the filter‘s doFilter method have exited or after a timeout period has passed. After the web container calls this method, it will not call the doFilter method again on this instance of the filter.
This method gives the filter an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the filter‘s current state in memory.
Example:
web.xml
1 <filter> 2 <filter-name>filter1</filter-name> 3 <filter-class>com.filter.FilterOne</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>filter1</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping> 9 10 <filter> 11 <filter-name>filter2</filter-name> 12 <filter-class>com.filter.FilterTwo</filter-class> 13 </filter> 14 <filter-mapping> 15 <filter-name>filter2</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping>
FilterOne.java
1 public class FilterOne implements Filter { 2 3 //Filter 链的执行 4 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) 5 throws IOException, ServletException { 6 // 对请求进行过滤 7 System.out.println("filter one before"); 8 9 // 下一个过滤器或 10 arg2.doFilter(arg0, arg1); 11 12 // 对响应的过滤 13 System.out.println("filter one after"); 14 } 15 16 // 初始化过滤器 17 public void init(FilterConfig arg0) throws ServletException { 18 System.out.println("filter one init"); 19 } 20 21 // 释放过滤器使用的资源 22 public void destroy() { 23 System.out.println("filter one destroy"); 24 } 25 26 }
输出:
filter one before
filter two before
filter two after
filter one after
标签:
原文地址:http://www.cnblogs.com/luangeng/p/5624976.html