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 method of the FilterChain. After printing the report, the filter calls the doFilter method of the FilterChain 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 moreservlets.filters. LogFilter. Next, the filter-mapping element uses a url-pattern of /* to associate the filter with all URLs in the Web application. See Listing 9.8.

  5. Disablement of the invoker servlet. This operation is shown in Section 9.2 and is not repeated here.

After the Web application is deployed on an external server and the logging filter is attached, a client request for the Web application home page results in an entry in the log file like “audits.irs.gov tried to access http://www.filtersrus.com/filters/ index.jsp on Fri Oct 26 15:16:15 EDT 2001. (Reported by Logger.)”

Listing 9.7. LogFilter.java
package moreservlets.filters; 

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.util.*; // For Date class 

/** Simple filter that prints a report in the log file 
 *  whenever the associated servlets or JSP pages 
 *  are accessed. 
 */ 

public class LogFilter implements Filter {
  protected FilterConfig config; 
  private ServletContext context; 
  private String filterName; 

  public void doFilter(ServletRequest request, 
                       ServletResponse response, 
                       FilterChain chain) 
      throws ServletException, IOException {
    HttpServletRequest req = (HttpServletRequest)request; 
    context.log(req.getRemoteHost() + 
                " tried to access " + 
                req.getRequestURL() + 
                " on " + new Date() + ". " + 
                "(Reported by " + filterName + ".)"); 
    chain.doFilter(request,response); 
  } 

  public void init(FilterConfig config)
						throws ServletException {
						this.config = config; // In case it is needed by subclass.
						context = config.getServletContext();
						filterName = config.getFilterName();
						} 

  public void destroy() {} 
} 

Listing 9.8. web.xml (Excerpt for logging filter)
<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 

<web-app> 
  <!-- ... --> 

  <!-- Register the name "Logger" for LogFilter. --> 
  <filter>
						<filter-name>Logger</filter-name>
						<filter-class>
						moreservlets.filters.LogFilter
						</filter-class>
						</filter> 
  <!-- ... --> 

  <!-- Apply the Logger filter to all servlets and 
       JSP pages. 
  --> 
  <filter-mapping>
						<filter-name>Logger</filter-name>
						<url-pattern>/*</url-pattern>
						</filter-mapping> 

  <!-- ... --> 
</web-app> 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.86