filter用来处理对用户请求的响应
1)创建filter首先要创建一个filter处理类,然后再在web.xml里进行配置。一个filter处理类需要实现javax.servlet.Filter接口
2)此接口中定义了3个方法 doFilter destroy init
3)
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());
//Fileter只是链式处理,请求依然放行到目的地址
chain.doFilter(request,response);
//下面的代码用于对服务器响应执行后处理
long after = System.currentTimeMillis();
//输出提示信息
System.out.println("过滤结束");
//输出提示信息
System.out.println("请求被定位到"+hrequest.getRequestURL() + "所话的事件为:"+(after - before));
}
}
filter对请求进行了处理和分发