This is only an optional operation , You can use this operation to see which connection is the most time-consuming , To find out the bottleneck of the system , Solve related performance problems .
To say , The use of filters is simple , There will be no burden here , Mainly about the specific implementation methods .
Here I only print requests that take more than two seconds , And do not process resource requests .
The first is configuration web.xml:
<!-- Time consuming statistical filter Start --><filter> <filter-name>FilterAll</filter-name> <filter-class>cn.com.vogue.filter.URLFilter</filter-class></filter><filter-mapping> <filter-name>FilterAll</filter-name> <url-pattern>/*</url-pattern></filter-mapping><!-- Time consuming statistical filter end -->
The second is to write filter code :
package cn.com.vogue.filter;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 org.slf4j.Logger;import org.slf4j.LoggerFactory;public class URLFilter implements Filter{ private static Logger logger = LoggerFactory.getLogger(URLFilter.class); public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); HttpServletRequest httpRequest = (HttpServletRequest)request; String url = httpRequest.getServletPath(); if(checkUrl(url)){ Date time_begin = new Date(); chain.doFilter(request, response); // Carry on Date time_end = new Date(); long i = (long) ((time_end.getTime() - time_begin.getTime())) / 1000; if(i > 2) logger.warn("===>>> The request takes time " + i + " second URL:" + url + " <<<==="); }else{ chain.doFilter(request, response); // Carry on } } /** * First level verification */ public boolean checkUrl(String url){ boolean isSucc = true; try { // Do not block login requests if("/login".equals(url)){ return false; } int pointIndex = url.lastIndexOf("."); if(pointIndex > 0){ String relUrl = url.substring(pointIndex, url.length()); if(".css".equals(relUrl)){ return false; } if(".jpg".equals(relUrl)){ return false; } if(".gif".equals(relUrl)){ return false; } if(".png".equals(relUrl)){ return false; } if(".ico".equals(relUrl)){ return false; } if(".js".equals(relUrl)){ return false; } if(".jsp".equals(relUrl)){ return false; } } } catch (Exception e) { logger.error(" The system made an error in resource level verification !", e); } return isSucc; } public void init(FilterConfig filterConfig) throws ServletException { }}
If there is a connection that takes too long to request , Will print time-consuming and connected .
But notice , We received this request , No parameters , Just the path of the specific request .
I recommend you to read more about “ ” The article