欢迎来到天天文库
浏览记录
ID:6352802
大小:609.88 KB
页数:14页
时间:2018-01-11
《java-filter简明教程》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、java-filter简明教程1.概念执行顺序监听排在最前面、最后面是servlet下面是初始化的顺序:多个filter的初始化顺序:2.编写3个测试类一个编码过滤器、日志过滤器、一个测试用的servlet测试目的:发现执行filter的顺序,init方法的执行时间destroy方法的执行时间,dofilter如何执行EncodingFilter:packagecom;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;im
2、portjavax.servlet.ServletResponse;importjavax.servlet.Filter;importjavax.servlet.FilterConfig;importjavax.servlet.FilterChain;importjava.io.IOException;importjavax.servlet.ServletContext;importjava.util.HashMap;importjava.util.Enumeration;importjava.util.Map;p
3、ublicclassEncodingFilterimplementsFilter{privateStringencoding;privateMapparams=newHashMap();//项目结束时就已经进行销毁publicvoiddestroy(){System.out.println("enddotheencodingfilter!");params=null;encoding=null;}publicvoiddoFilter(ServletRequ
4、estreq,ServletResponseresp,FilterChainchain)throwsIOException,ServletException{//UtilTimerStack.push("EncodingFilter_doFilter:");System.out.println("beforeencoding"+encoding+"filter!");req.setCharacterEncoding(encoding);//resp.setCharacterEncoding(encoding);//
5、resp.setContentType("text/html;charset="+encoding);chain.doFilter(req,resp);System.out.println("afterencoding"+encoding+"filter!");System.err.println("----------------------------------------");//UtilTimerStack.pop("EncodingFilter_doFilter:");}//项目启动时就已经进行读取pu
6、blicvoidinit(FilterConfigconfig)throwsServletException{System.out.println("begindotheencodingfilter!");encoding=config.getInitParameter("encoding");for(Enumeratione=config.getInitParameterNames();e.hasMoreElements();){Stringname=(String)e.nextElement();Stringv
7、alue=config.getInitParameter(name);params.put(name,value);}}}LogFilter:packagecom;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.Filter;importjavax.servlet.FilterConfig;importjava
8、x.servlet.FilterChain;importjava.io.IOException;importjavax.servlet.ServletContext;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publiccl
此文档下载收益归作者所有