import java.io.IOException; import java.util.Date; 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; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; @Component // 表明作为spring的一个bean public class TimeFilter implements Filter { private HttpServletRequest request; private HttpServletResponse response; @Override public void destroy() { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { this.request = (HttpServletRequest) servletRequest; this.response = ((HttpServletResponse) servletResponse); long start = new Date().getTime(); // 过滤器主要逻辑,整个处理流程 chain.doFilter(request, response); System.out.print(request.getRequestURL()); System.out.println(" time filter 耗时:" + (new Date().getTime() - start)+"ms"); ; } @Override public void init(FilterConfig arg0) throws ServletException { } }