Learn Java for Web Development

(Tina Meador) #1

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 element or in the @WebFilterannotation (introduced in the section that follows). In the


element, you must declare the following:
   <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



ResponseFilter
com.apress.ResponseServlet

Filters can be associated with a servlet using the element. Listing 2-10 maps the
Response Filter filter to the ResponseServlet servlet.


Listing 2-10. Mapping the Filter to the Servlet



Response Filter
ResponseServlet

Filters can be associated with groups of servlets using , as illustrated in Listing 2-11.


Listing 2-11. Associating a Filter with a Group of Servlets



Response Filter
/*

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
Free download pdf