66 CHAPTER 2: Building Web Applications Using Servlets and JSP
Providing actions on web components by using a chain of filters in a
specific order
Modifying the response headers and response data before they are rendered
A filter is created by implementing the javax.servlet.Filter interface and providing a no-arg
constructor. A filter is configured in a web application either in the deployment descriptor using
the
<filter-name>: Used to map the filter to a servlet or URL
<filter-class>: Used by the container to identify the filter type
Note You can also declare the initialization parameters for a filter.
Listing 2-9 illustrates the filter declaration.
Listing 2-9. Declaring the Filter
Filters can be associated with a servlet using the
Response Filter filter to the ResponseServlet servlet.
Listing 2-10. Mapping the Filter to the Servlet
Filters can be associated with groups of servlets using
Listing 2-11. Associating a Filter with a Group of Servlets
In Listing 2-11 the response filter is applied to all the servlets in the web application.
A web application typically comprises the following filtering components:
Authentication filters
Caching filters