Architecture of a response filter

Rachel is talking about the basic structure of what you put in a doFilter() method—first you do work related to the request, then you call chain.doFilter(), then finally, when the servlet (and any other filter in the chain after your filter) completes and control is returned to your original doFilter()method, you can do something to the response.

Rachel’s pseudo-code for the compression filter

image with no caption

The conceptual call stack

The Compression filter’s doFilter() method runs, and invokes chain.doFilter(). It’s too early to do any compression—the goal is to compress the response output from the servlet.

The Servlet’s service() method goes on the top of the stack, does some work, generates a response output, and completes.

Now that the Servlet’s service() method has popped off the stack, the rest of the compression filter’s doFilter() method can run, and (it hopes) do the compression on whatever the servlet wrote to the response output!

Get Head First Servlets and JSP, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.