Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Filter code with Servlet 2.3 model

Discover freely available servlet filters you can use today

Page 2 of 6

Spot the slowdown

To really understand filters, you have to see them in action. The first filter we'll look at is simple but powerful; it records the duration of all requests. It's modeled loosely after the nondescriptively named ExampleFilter from the Tomcat 4.0 distribution. Here is the code:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TimerFilter implements Filter {
  private FilterConfig config = null;
  public void init(FilterConfig config) throws ServletException {
    this.config = config;
  }
  public void destroy() {
    config = null;
  }
  public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {
    long before = System.currentTimeMillis();
    chain.doFilter(request, response);
    long after = System.currentTimeMillis();
    String name = "";
    if (request instanceof HttpServletRequest) {
      name = ((HttpServletRequest)request).getRequestURI();
    }
    config.getServletContext().log(name + ": " + (after - before) + "ms");
  }
}


When the server calls init(), the filter saves a reference to the config in its config variable, which is later used in the doFilter() method to retrieve the ServletContext. When the server calls doFilter(), the filter times how long the request handling takes and logs the time once processing has completed. This filter nicely demonstrates before- and after-request processing. Notice that the parameters to the doFilter()method are not HTTP-aware objects, so to call the HTTP-specific getRequestURI() method requires a cast of the request to an HttpServletRequest type.

To use this filter, you must declare it in the web.xmldeployment descriptor using the <filter>tag, as shown below:

<
filter>
    <filter-name>timerFilter</filter-name>
    <filter-class>TimerFilter</filter-class>
</filter>


This notifies the server that a filter named timerFilteris implemented in the TimerFilterclass. You can apply a registered filter to certain URL patterns or servlet names using the <filter-mapping>tag:

<
filter-mapping>
    <filter-name>timerFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


This configures the filter to operate on all requests to the server (static or dynamic), just what we want for our timing filter. If you connect to a simple page, the log output might look like this:

2001-05-25 00:14:11 /timer/index.html: 10ms


In Tomcat 4.0 beta 5, you'll find the log file under server_root/logs/.

Download the WAR file for this filter at http://www.javaworld.com/jw-06-2001/Filters/timer.war.

Who's on your site, and what are they doing?

Our next filter is a clickstream filter written by the folks at OpenSymphony. This filter tracks user requests (a.k.a. clicks) and request sequences (a.k.a. clickstreams) to show a site administrator who's visiting her site and what pages each visitor has accessed so far. It's an open source library, under the LGPL license.

Resources