Servlet Filtering

Filters are a feature that was introduced in version 2.3 of the servlet specification. A filter is a special type of servlet that dynamically intercepts requests and responses and transforms the information contained in them. The main advantage of a filter is that it can be added to existing applications without any need for recompilation.

Filters can have several important uses:

  • To encapsulate recurring tasks in reusable units

  • To format the data sent back to the client

  • To provide authorization and blocking of requests

  • To provide logging and auditing

A filter can perform filtering tasks on the request, the response, or both.

Programming Filters

A filter is still a servlet, so it extends HttpServlet but it also implements the

Get Sams Teach Yourself J2EE™ in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.