..
}
Chapter 5 ?– Servlet and JSP Filters 218
5.4 Example: A Logging Filter
Let??™s update the ReportFilter (Listing 5.2) so that messages go in the log file
instead of to the standard output. To accomplish this task, we implement the following
capabilities.
1. A class that implements the Filter interface. This class is called
LogFilter and is shown in Listing 5.7. The init method of this
class stores the FilterConfig, ServletContext, and filter name
in fields of the filter. The class provides an empty body for the
destroy method.
2. Filtering behavior in the doFilter method. There are two differences
between this behavior and that of the ReportFilter: The
report is placed in the log file instead of the standard output and the
report includes the name of the filter.
3. A call to the doFilter method of the FilterChain. After printing
the report, the filter calls the doFilter method of the Filter-
Chain to invoke the next filter in the chain (or the servlet or JSP page
if there are no more filters).
4. Registration with all URLs. First, the filter element associates
the name LogFilter with the class coreservlets.filters.
LogFilter. Next, the filter-mapping element uses a urlpattern
of /* to associate the filter with all URLs in the Web application.
See Listing 5.8.
5. Disablement of the invoker servlet. This operation is shown in
Section 5.2 (Example: A Reporting Filter) and is not repeated here.
Pages:
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268