Servlet中的过滤器Filter详解
1.过滤器的概念
Java中的Filter并不是一个标准的Servlet,它不能处理用户请求,也不能对客户端生成响应。主要用于对HttpServletRequest进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链。
优点:过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别注意过滤链的执行顺序问题
2.过滤器的作用描述
在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest。
根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。
在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
根据需要检查HttpServletResponse,可以修改HttpServletResponse头和数据。
3.过滤器的执行流程
4.Filter接口
1.如何驱动
在web应用程序启动时,web服务器将根据web.xml文件中的配置信息来创建每个注册的Filter实例对象,并将其保存在服务器的内存中
2.方法介绍
init()Init方法在Filter生命周期中仅执行一次,web容器在调用init方法时
destory()在Web容器卸载Filter对象之前被调用。该方法在Filter的生命周期中仅执行一次。在这个方法中,可以释放过滤器使用的资源。
doFilter()Filter链的执行
5.FilterChain接口
1.如何实例化
代表当前Filter链的对象。由容器实现,容器将其实例作为参数传入过滤器对象的doFilter()方法中
2.作用
调用过滤器链中的下一个过滤器
filter实例:
web.xml配置
[html]viewplaincopyprint?
setCharacterEncoding
com.company.strutstudy.web.servletstudy.filter.EncodingFilter
encoding
utf-8
setCharacterEncoding
/
logfilter
com.company.strutstudy.web.servletstudy.filter.LogFilter
logfilter
/
编码拦截器:
[java]viewplaincopyprint?
publicclassEncodingFilterimplementsFilter{
privateStringencoding;
privateMapparams=newHashMap();
//项目结束时就已经进行销毁
publicvoiddestroy(){
System.out.println("enddotheencodingfilter!");
params=null;
encoding=null;
}
publicvoiddoFilter(ServletRequestreq,ServletResponseresp,
FilterChainchain)throwsIOException,ServletException{
//UtilTimerStack.push("EncodingFilter_doFilter:");
System.out.println("beforeencoding"+encoding+"filter!");
req.setCharacterEncoding(encoding);
//resp.setCharacterEncoding(encoding);
//resp.setContentType("text/html;charset="+encoding);
chain.www.wang027.comdoFilter(req,resp);
System.out.println("afterencoding"+encoding+"filter!");
System.err.println("----------------------------------------");
//UtilTimerStack.pop("EncodingFilter_doFilter:");
}
//项目启动时就已经进行读取
publicvoidinit(FilterConfigconfig)throwsServletException{
System.out.println("begindotheencodingfilter!");
encoding=config.getInitParameter("encoding");
for(Enumeratione=config.getInitParameterNames();e
.hasMoreElements();){
Stringname=(String)e.nextElement();
Stringvalue=config.getInitParameter(name);
params.put(name,value);
}
}
}
日志拦截器:
[java]viewplaincopyprint?
publicclassLogFilterimplementsFilter{
FilterConfigconfig;
publicvoiddestroy(){
this.config=null;
}
publicvoiddoFilter(ServletRequestreq,ServletResponseres,
FilterChainchain)throwsIOException,ServletException{
//获取ServletContext对象,用于记录日志
ServletContextcontext=this.config.getServletContext();
//longbefore=System.currentTimeMillis();
System.out.println("beforethelogfilter!");
//context.log("开始过滤");
//将请求转换成HttpServletRequest请求
HttpServletRequesthreq=(HttpServletRequest)req;
//记录日志
System.out.println("LogFilter已经截获到用户的请求的地址:"+hreq.getServletPath());
//context.log("Filter已经截获到用户的请求的地址:"+hreq.getServletPath());
try{
//Filter只是链式处理,请求依然转发到目的地址。
chain.doFilter(req,res);
}catch(Exceptione){
e.printStackTrace();
}
System.out.println("afterthelogfilter!");
//longafter=System.currentTimeMillis();
//记录日志
//context.log("过滤结束");
//再次记录日志
//context.log("请求被定位到"+((HttpServletRequest)req).getRequestURI()
//+"所花的时间为:"+(after-before));
}
publicvoidinit(FilterConfigconfig)throwsServletException{
System.out.println("begindothelogfilter!");
this.config=config;
}
}
HelloServlet类:
[java]viewplaincopyprint?
publicclassHelloWorldServletextendsHttpServlet{
/
查看httpservlet实现的service一看便知,起到了一个controll控制器的作用(转向的)
之后便是跳转至我们熟悉的doget,dopost等方法中
/
@Override
protectedvoidservice(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
System.out.println("doservice..."+this.getInitParameter("encoding"));
super.service(req,resp);
}
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
System.out.println("doget...");
doPost(req,resp);
}
@Override
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
System.out.println("dopost...");
}
}
结果:
[plain]viewplaincopyprint?
beforeencodingutf-8filter!
beforethelogfilter!
LogFilter已经截获到用户的请求的地址:/hello
doservice...UTF-8
doget...
dopost...
afterthelogfilter!
afterencodingutf-8filter!
----------------------------------------
总结:
1.过滤器执行流程
2.常用过滤器
[html]viewplaincopyprint?
|
|