9.2. Example: A Reporting Filter

Just to warm up, let’s try a simple filter that merely prints a message to standard output whenever the associated servlet or JSP page is invoked. To accomplish this task, the filter has the following capabilities.

  1. A class that implements the Filter interface. This class is called ReportFilter and is shown in Listing 9.2. The class provides empty bodies for the init and destroy methods.

  2. Filtering behavior in the doFilter method. Each time a servlet or JSP page associated with this filter is invoked, the doFilter method generates a printout that lists the requesting host and the URL that was invoked. Since the getRequestURL method is in HttpServletRequest, not ServletRequest, I cast the ServletRequest object ...

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.