import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.security.*;
public class MyServlet implements Filter
{
private FilterConfig filterConfig = null;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
long start = System.currentTimeMillis();
String address = request.getRemoteAddr();
String file = ((HttpServletRequest) request).getRequestURI();
chain.doFilter(request, response);
filterConfig.getServletContext().log(
"User access! " +
" User IP: " + address +
" Resource: " + file +
" Milliseconds used: " + (System.currentTimeMillis() - start)
);
}
public void destroy() { }
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<filter>
<filter-name>Simple Filter</filter-name>
<filter-class>MyServlet</filter-class>
<init-param>
<param-name>message</param-name>
<param-value>Hello!</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Simple Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Download: ServletFilterConfigLog.zip( 89 k)