1、多个Filter的执行顺序
Fileter的执行顺序与在web.xml中声明的顺序一致,先声明的先执行。
- <filter>
- <filter-name>MyServlet1</filter-name>
- <filter-class>com.alibaba.exchange.MyServlet1</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>MyServlet1</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <filter>
- <filter-name>MyServlet2</filter-name>
- <filter-class>com.alibaba.exchange.MyServlet2</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>MyServlet2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
在Filter中代码如下:
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- // TODO Auto-generated method stub
- System.out.println("MyServlet1 doFilter before");
- chain.doFilter(request, response);
- System.out.println("MyServlet1 doFilter after");
- }
过滤链执行结果如下:
- MyServlet1 doFilter before
- MyServlet2 doFilter before
- zhangsan, hello123
- MyServlet2 doFilter after
- MyServlet1 doFilter after
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/678234,如需转载请自行联系原作者