logFilter Analysis
分析下面这段代码:
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain) throws IOException, ServletException {
//--------下面的代码用于对用户请求执行预处理---------
//获取ServletContext对象,用于记录日志
ServletContext context=this.config.getServletContext();
long before = System.currentTimeMillis();
System.out.println("开始过滤...");
//将请求转换为HttpServletRequest请求
HttpServletRequest hRequest = (HttpServletRequest) request;
//输出提示信息
System.out.println("Filter 已经截获到用户的请求地址: " + hRequest.getServletPath());
//Filter只是链式处理,请求依然放行到目的地址
chain.doFilter(request, response);
long after = System.currentTimeMillis();
System.out.println("请求被定位到"+hRequest.getRequestURI()+" 所花时间为:"+(after-before));
}
- getServletPath()用户的请求地址
- getRequestURI()请求的url路径,例如此处的答应信息为/{应用名}/demo.jsp
- HttpServletRequest hRequest = (HttpServletRequest) request; 将请求转换为HttpServletRequest请求
- ServletContext context=this.config.getServletContext(); 获取ServletContext对象,用于记录日志