Servlet Log Filter : Log « Servlet « Java Tutorial






import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet implements Filter {

  FilterConfig config;

  public void init(FilterConfig config){
    
  }

  public void setFilterConfig(FilterConfig config) {
    this.config = config;
  }

  public FilterConfig getFilterConfig() {
    return config;
  }

  public void doFilter(ServletRequest req,
                       ServletResponse res,
                       FilterChain chain) {
//    ServletContext context = getFilterConfig().getServletContext();
  //  long bef = System.currentTimeMillis();
    //chain.doFilter(req, res); // no chain parameter needed here
//    long aft = System.currentTimeMillis();
    //context.log("Request to " + req.getRequestURI() + ": " +(aft-bef));
    
    System.out.println("logging");
  }
  public void destroy(){}
}
<?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>

    <filter>
        <filter-name>
            log
        </filter-name>
        <filter-class>
            MyServlet
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
  Download:  ServletLogFilter.zip( 1,066 k)








25.26.Log
25.26.1.Servlet Log Filter
25.26.2.Servlet Context Log