filter和servlet的执行顺序关系

先走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
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页