background image

Filter-to-Servlet Mapping

<< Applying the Filter to Requests | Invoking OtherWeb Resources >>
<< Applying the Filter to Requests | Invoking OtherWeb Resources >>

Filter-to-Servlet Mapping

You can direct the filter to be applied to any combination of the preceding situations by
including multiple dispatcher elements. If no elements are specified, the default option is
REQUEST
.
If you want to log every request to a web application, you map the hit counter filter to the URL
pattern /*.
Table 4­6
summarizes the filter definition and mapping list for the Duke's Bookstore
application. The filters are matched by servlet name, and each filter chain contains only one
filter.
TABLE 4­6
Duke's Bookstore Filter Definition and Mapping List
Filter
Class
Servlet
HitCounterFilter
filters.HitCounterFilter
BookStoreServlet
OrderFilter
filters.OrderFilter
ReceiptServlet
You can map a filter to one or more web resources and you can map more than one filter to a
web resource. This is illustrated in
Figure 4­4
, where filter F1 is mapped to servlets S1, S2, and
S3, filter F2 is mapped to servlet S2, and filter F3 is mapped to servlets S1 and S2.
Recall that a filter chain is one of the objects passed to the doFilter method of a filter. This
chain is formed indirectly by means of filter mappings. The order of the filters in the chain is the
same as the order in which filter mappings appear in the web application deployment
descriptor.
When a filter is mapped to servlet S1, the web container invokes the doFilter method of F1.
The doFilter method of each filter in S1's filter chain is invoked by the preceding filter in the
S1
S2
S3
F1
F2
F3
FIGURE 4­4
Filter-to-Servlet Mapping
Filtering Requests and Responses
Chapter 4 · Java Servlet Technology
121