先走filter,然后走servlet,然后回到filter,一个filter可以用chain.doFilter()分成前后两部分。
filterA:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(1);
chain.doFilter(request, response);
System.out.println(2);
}
filterB:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println(3);
chain.doFilter(request, response);
System.out.println(4);
}
servletA:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("来到servlet了");
response.getWriter().append("Served at: ").append(request.getContextPath());
}
输出结果是:
1
3
来到servlet了
4
2