filter的执行顺序

一直没有仔细去研究下filter ,最近系统的测试了下:

先看代码吧

Java代码   收藏代码
  1. FirstFilter.java  
  2. ==================  
  3. package com.test.filter;  
  4.   
  5. import java.io.IOException;  
  6.   
  7. import javax.servlet.Filter;  
  8. import javax.servlet.FilterChain;  
  9. import javax.servlet.FilterConfig;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.ServletRequest;  
  12. import javax.servlet.ServletResponse;  
  13.   
  14. public class FirstFilter implements Filter {  
  15.   
  16.     @Override  
  17.     public void destroy() {  
  18.   
  19.     }  
  20.   
  21.     @Override  
  22.     public void doFilter(ServletRequest request, ServletResponse response,  
  23.             FilterChain chain) throws IOException, ServletException {  
  24.         System.out.println("before invoke firstFilter's chain.doFilter() ..");  
  25.         chain.doFilter(request, response);  
  26.         System.out.println("after invoke firstFilter's chain.doFilter() ..");  
  27.     }  
  28.   
  29.     @Override  
  30.     public void init(FilterConfig arg0) throws ServletException {  
  31.         System.out.println("firstFilter init()...");  
  32.   
  33.     }  
  34.   
  35. }  
  36.   
  37.   
  38. ============  
  39.   
  40. SecondFilter.java  
  41. ==============  
  42.   
  43. package com.test.filter;  
  44.   
  45. import java.io.IOException;  
  46.   
  47. import javax.servlet.Filter;  
  48. import javax.servlet.FilterChain;  
  49. import javax.servlet.FilterConfig;  
  50. import javax.servlet.ServletException;  
  51. import javax.servlet.ServletRequest;  
  52. import javax.servlet.ServletResponse;  
  53.   
  54. public class SecondFilter implements Filter {  
  55.   
  56.     @Override  
  57.     public void destroy() {  
  58.   
  59.     }  
  60.   
  61.     @Override  
  62.     public void doFilter(ServletRequest request, ServletResponse response,  
  63.             FilterChain chain) throws IOException, ServletException {  
  64.         System.out.println("before invoke secondFilter's chain.doFilter() ..");  
  65.         chain.doFilter(request, response);  
  66.         System.out.println("after invoke secondFilter's chain.doFilter() ..");  
  67.     }  
  68.   
  69.     @Override  
  70.     public void init(FilterConfig filterConfig) throws ServletException {  
  71.         System.out.println("secondFilter init()...");  
  72.     }  
  73.   
  74. }  
  75. ==========  
  76.   
  77. FirstServlet.java  
  78. =============  
  79. package com.test.servlet;  
  80.   
  81. import java.io.IOException;  
  82.   
  83. import javax.servlet.ServletException;  
  84. import javax.servlet.http.HttpServlet;  
  85. import javax.servlet.http.HttpServletRequest;  
  86. import javax.servlet.http.HttpServletResponse;  
  87.   
  88. public class FirstServlet extends HttpServlet {  
  89.   
  90.     @Override  
  91.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  92.             throws ServletException, IOException {  
  93.         System.out.println("servlet doGet be invoked...");  
  94.         req.getRequestDispatcher("test.jsp").forward(req, resp);  
  95.     }  
  96.   
  97.     @Override  
  98.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  99.             throws ServletException, IOException {  
  100.         // TODO Auto-generated method stub  
  101.         doGet(req, resp);  
  102.     }  
  103.       
  104. }  
  105.   
  106. =========  
  107.   
  108. web.xml  
  109.   
  110. ===========  
  111. <?xml version="1.0" encoding="UTF-8"?>  
  112. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  113.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  114.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  115.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  116.     <welcome-file-list>  
  117.         <welcome-file>index.jsp</welcome-file>  
  118.     </welcome-file-list>  
  119.     <filter>  
  120.         <filter-name>firstFilter</filter-name>  
  121.         <filter-class>com.test.filter.FirstFilter</filter-class>  
  122.     </filter>  
  123.     <filter>  
  124.         <filter-name>secondFilter</filter-name>  
  125.         <filter-class>com.test.filter.SecondFilter</filter-class>  
  126.     </filter>  
  127.     <filter-mapping>  
  128.         <filter-name>secondFilter</filter-name>  
  129.         <url-pattern>/*</url-pattern>  
  130.     </filter-mapping>  
  131.     <filter-mapping>  
  132.         <filter-name>firstFilter</filter-name>  
  133.         <url-pattern>/*</url-pattern>  
  134.     </filter-mapping>  
  135.   
  136.     <servlet>  
  137.         <servlet-name>firstServlet</servlet-name>  
  138.         <servlet-class>com.alimama.servlet.FirstServlet</servlet-class>  
  139.     </servlet>  
  140.     <servlet-mapping>  
  141.         <servlet-name>firstServlet</servlet-name>  
  142.         <url-pattern>/firstServlet</url-pattern>  
  143.     </servlet-mapping>  
  144. </web-app>  
  145.   
  146. ============  

 然后发布,发现打印的日志如下:

。。。

firstFilter init()...
secondFilter init()...

。。。

2008-10-23 11:36:43 org.apache.catalina.startup.Catalina start
信息: Server startup in 3665 ms

 

这里过滤器初始化好了。

当我们访问我们的 应用:http://127.0.0.1:8080/appName

发现打印日记如下:

before invoke secondFilter's chain.doFilter() ..
before invoke firstFilter's chain.doFilter() ..
after invoke firstFilter's chain.doFilter() ..
after invoke secondFilter's chain.doFilter() ..

 

当我们将web.xml中filter的位置进行调整后:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.     <filter>  
  10.         <filter-name>firstFilter</filter-name>  
  11.         <filter-class>com.test.filter.FirstFilter</filter-class>  
  12.     </filter>  
  13.     <filter>  
  14.         <filter-name>secondFilter</filter-name>  
  15.         <filter-class>com.test.filter.SecondFilter</filter-class>  
  16.     </filter>  
  17.    <filter-mapping>  
  18.         <filter-name>firstFilter</filter-name>  
  19.         <url-pattern>/*</url-pattern>  
  20.     </filter-mapping>  
  21.     <filter-mapping>  
  22.         <filter-name>secondFilter</filter-name>  
  23.         <url-pattern>/*</url-pattern>  
  24.     </filter-mapping></span>  
  25.       
  26.     <servlet>  
  27.         <servlet-name>firstServlet</servlet-name>  
  28.         <servlet-class>com.alimama.servlet.FirstServlet</servlet-class>  
  29.     </servlet>  
  30.     <servlet-mapping>  
  31.         <servlet-name>firstServlet</servlet-name>  
  32.         <url-pattern>/firstServlet</url-pattern>  
  33.     </servlet-mapping>  
  34. </web-app>  

  然后在启动应用,会看到打印:

before invoke firstFilter's chain.doFilter() ..

before invoke secondFilter's chain.doFilter() ..
after invoke secondFilter's chain.doFilter() ..

after invoke firstFilter's chain.doFilter() ..

总结:

filter的执行顺序是按照web.xml中定义的顺序进行执行的(当多个filter都匹配的时候)。

你可能感兴趣的:(filter的执行顺序)