WebFilter
2020-12-17 17:33
标签:res null his java 方法 gets 转换 after 对象 WebFilter 标签:res null his java 方法 gets 转换 after 对象 原文地址:https://www.cnblogs.com/hshy/p/14070792.htmlpackage test.filter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class LogFilter implements Filter {
private FilterConfig config;
// 实现初始化方法
public void init(FilterConfig config) {
this.config = config;
}
// 实现销毁方法
public void destroy() {
this.config = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) {
// 获取ServletContext 对象,用于记录日志
ServletContext context = this.config.getServletContext();
long before = System.currentTimeMillis();
System.out.println("开始过滤... ");
// 将请求转换成HttpServletRequest 请求
HttpServletRequest hrequest = (HttpServletRequest) request;
// 记录日志
context.log("Filter已经截获到用户的请求的地址: " + hrequest.getServletPath());
try {
// Filter 只是链式处理,请求依然转发到目的地址。
chain.doFilter(request, response);
} catch (Exception e) {
e.printStackTrace();
}
long after = System.currentTimeMillis();
// 记录日志
context.log("过滤结束");
// 再次记录日志
context.log(" 请求被定位到" + ((HttpServletRequest) request).getRequestURI()
+ "所花的时间为: " + (after - before));
}
}