filter的执行顺序

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

先看代码吧

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

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

。。。

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的位置进行调整后:

  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. <servlet>
  26. <servlet-name>firstServlet</servlet-name>
  27. <servlet-class>com.alimama.servlet.FirstServlet</servlet-class>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>firstServlet</servlet-name>
  31. <url-pattern>/firstServlet</url-pattern>
  32. </servlet-mapping>
  33. </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都匹配的时候)。

上一篇:wpfのuri(让你完全明白wpf的图片加载方式以及URI写法)


下一篇:MySQL(二) —— 数据类型与操作数据表