当有多个Filter过滤一个资源时,执行顺序到底是怎样?
如下测试:
定义两个Filter,FirstFilter,SecondFilter,它们过滤同一个index.jsp页面,并且SecondFilter的映射写在FirstFilter后面
并在两个Filter的doFilter()方法中加上输出语句:
例如FirstFilter的doFilter方法:
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("FirstFilter doFilter begin--------------------1"); chain.doFilter(request, response); System.out.println("FirstFilter doFilter end --------------------2"); }
同理SecondFilter,标上3,4
在index.jsp页面中写Java代码输出5:<% System.out.println("index.jsp--------------------------------------5");%>
启动tomcat后,访问index.jsp,控制台输出如下:
FirstFilter doFilter begin--------------------1
SecondFilter doFilter begin---------------------3
index.jsp--------------------------------------5
SecondFilter doFilter end ---------------------4
FirstFilter doFilter end --------------------2
从上面结果看来,不难看出执行顺序了