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 javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation...