Java Reference
In-Depth Information
Providing actions on web components by using a chain of filters in a
specific order
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> element or in the @WebFilter annotation (introduced in the section that follows). In the
<filter> 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
Modifying the response headers and response data before they are rendered
Note
You can also declare the initialization parameters for a filter.
Listing 2-9 illustrates the filter declaration.
Listing 2-9. Declaring the Filter
<filter>
<filter-name>ResponseFilter</filter-name>
<filter-class>com.apress.ResponseServlet</filter-class>
</filter>
Filters can be associated with a servlet using the <servlet-name> element. Listing 2-10 maps the
Response Filter filter to the ResponseServlet servlet.
Listing 2-10. Mapping the Filter to the Servlet
<filter-mapping>
<filter-name>Response Filter</filter-name>
<servlet-name>ResponseServlet</servlet-name>
</filter-mapping>
Filters can be associated with groups of servlets using <urlpattern> , as illustrated in Listing 2-11.
Listing 2-11. Associating a Filter with a Group of Servlets
<filter-mapping>
<filter-name>Response Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
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
 
 
Search WWH ::




Custom Search