欢迎来到天天文库
浏览记录
ID:14221818
大小:41.00 KB
页数:13页
时间:2018-07-27
《五个有用的过滤器》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、五个有用的过滤器出自:http://xxtianxiaxing.javaeye.com/blog/350161在于自己收集好的东西,谢谢作者。Java代码五个有用的过滤器一、使浏览器不缓存页面的过滤器importjavax.servlet.*;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/***用于的使Browser不缓存页面的过滤器*/publicclassForceNoCacheFilterimplementsFilter{publi
2、cvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain)throwsIOException,ServletException{((HttpServletResponse)response).setHeader("Cache-Control","no-cache");((HttpServletResponse)response).setHeader("Pragma","no-cache");((HttpServletRespon
3、se)response).setDateHeader("Expires",-1);filterChain.doFilter(request,response);}publicvoiddestroy(){}publicvoidinit(FilterConfigfilterConfig)throwsServletException{}}二、检测用户是否登陆的过滤器importjavax.servlet.*;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.htt
4、p.HttpServletResponse;importjavax.servlet.http.HttpSession;importjava.util.List;importjava.util.ArrayList;importjava.util.StringTokenizer;importjava.io.IOException;/***用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面*配置参数*checkSessionKey需检查的在Session中保存的关键字*redirectURL如果用户未登录,则重定向到指定的页面
5、,URL不包括ContextPath*notCheckURLList不做检查的URL列表,以分号分开,并且URL中不包括ContextPath*/publicclassCheckLoginFilterimplementsFilter{protectedFilterConfigfilterConfig=null;privateStringredirectURL=null;privateListnotCheckURLList=newArrayList();privateStringsessionKey=null;publicvoiddoF
6、ilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{HttpServletRequestrequest=(HttpServletRequest)servletRequest;HttpServletResponseresponse=(HttpServletResponse)servletResponse;HttpSessionsession=r
7、equest.getSession();if(sessionKey==null){filterChain.doFilter(request,response);return;}if((!checkRequestURIIntNotFilterList(request))&&session.getAttribute(sessionKey)==null){response.sendRedirect(request.getContextPath()+redirectURL);return;}filterChain.doFilter(servl
8、etRequest,servletResponse);}publicvoiddestroy(){notCheckURLList.clear();}privatebooleancheckRequestURIIntNotFi
此文档下载收益归作者所有