9.4. Example: A Logging Filter

Let’s update the ReportFilter (Listing 9.2) so that messages go in the log file instead of to the standard output. To accomplish this task, the filter has the following capabilities.

  1. A class that implements the Filter interface. This class is called LogFilter and is shown in Listing 9.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

Get More Servlets and JavaServer Pages™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.