Simple filter for measuring servlet response times : Filter « Servlet « Java Tutorial


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class ResponseTimerFilter implements Filter {
  protected FilterConfig config;

  public void init(FilterConfig config) throws ServletException {
    this.config = config;

  public void destroy() {

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    chain.doFilter(request, response);
    long elapsed = System.currentTimeMillis() - startTime;
    String name = "servlet";
    if (request instanceof HttpServletRequest) {
      name = ((HttpServletRequest) request).getRequestURI();

    config.getServletContext().log(name + " took " + elapsed + " ms");
  <filter-name>Timing Filter</filter-name>

  <filter-name>Timing Filter</filter-name>


25.17.1.Your First Filter
25.17.2.Filter Response
25.17.3.Servlet Filter Init Parameter
25.17.4.Use Filter to log Configuration
25.17.5.Use Filter to check Request Parameter
25.17.6.Use Filter to Check Date
25.17.7.Simple filter for measuring servlet response times
25.17.8.Filter that uses a response wrapper to convert all output to uppercase