Is doFilter() executed before or after the Servlet’s work is done?

The filter chain in essence wraps the servlet invocation. The chain will process all links until it hits the “bottom”, then allow the servlet to run, and then return up the chain in reverse. For example, if you have a new “example filter”, your doFilter() method may look like this:

public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
// do pre-servlet work here
chain.doFilter(request, response);
// do post servlet work here

}

Leave a Comment