1、多个Filter的执行顺序

Fileter的执行顺序与在web.xml中声明的顺序一致,先声明的先执行。

   
   
   
   
  1. <filter> 
  2.         <filter-name>MyServlet1filter-name> 
  3.         <filter-class>com.alibaba.exchange.MyServlet1filter-class> 
  4.     filter> 
  5.     <filter-mapping> 
  6.         <filter-name>MyServlet1filter-name> 
  7.         <url-pattern>/*url-pattern> 
  8.     filter-mapping> 
  9.     <filter> 
  10.         <filter-name>MyServlet2filter-name> 
  11.         <filter-class>com.alibaba.exchange.MyServlet2filter-class> 
  12.     filter> 
  13.     <filter-mapping> 
  14.         <filter-name>MyServlet2filter-name> 
  15.         <url-pattern>/*url-pattern> 
  16.     filter-mapping> 

在Filter中代码如下:

   
   
   
   
  1. @Override 
  2. public void doFilter(ServletRequest request, ServletResponse response, 
  3.         FilterChain chain) throws IOException, ServletException { 
  4.     // TODO Auto-generated method stub 
  5.     System.out.println("MyServlet1 doFilter before"); 
  6.     chain.doFilter(request, response); 
  7.     System.out.println("MyServlet1 doFilter after"); 

过滤链执行结果如下:

    
    
    
    
  1. MyServlet1 doFilter before 
  2. MyServlet2 doFilter before 
  3. zhangsan, hello123
  4. MyServlet2 doFilter after 
  5. MyServlet1 doFilter after