package exa;
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");
}
}
過濾器是作用於所有的Action的,在進入action之前,或者之後對請求的預處理或者對響應結果的後處理的。
你可以對Action的響應作後處理,將你計算的執行時間放入response中,在浏覽器上顯示。