Servlet filters

Filters were introduced to the servlet specification in version 2.3. A filter is an object that can dynamically intercept a request and manipulate its data before the request is handled by the servlet. Filters can also manipulate a response after a servlet's doGet() or doPost() method finishes, but before the output is sent to the browser.

The only way to configure a filter in earlier servlet specifications was to use the <filter-mapping> tag in web.xml. Servlet 3.0 introduced the ability to configure servlets via the @WebFilter annotation.

The following example illustrates how to do this:

package net.ensode.javaee8book.simpleapp; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Filter; import ...

Get Java EE 8 Application Development 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.