To Log the time taken by a request we can use Timer Filter.It will help us to find out performance of system and finding out which request are taking more time then required.
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");
}
}