欢迎来到天天文库
浏览记录
ID:47105294
大小:19.00 KB
页数:8页
时间:2019-08-04
《JAVA五个有用的过滤器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、五个有用的过滤器一、使浏览器不缓存页面的过滤器importjavax.servlet.*;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;/***用于的使Browser不缓存页面的过滤器*/publicclassForceNoCacheFilterimplementsFilter{publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilte
2、rChain)throwsIOException,ServletException{((HttpServletResponse)response).setHeader("Cache-Control","no-cache");((HttpServletResponse)response).setHeader("Pragma","no-cache");((HttpServletResponse)response).setDateHeader("Expires",-1);filterChain.doFilter(request,respo
3、nse);}publicvoiddestroy(){}publicvoidinit(FilterConfigfilterConfig)throwsServletException{}}二、检测用户是否登陆的过滤器importjavax.servlet.*;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importjava.util
4、.List;importjava.util.ArrayList;importjava.util.StringTokenizer;importjava.io.IOException;/***用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面*配置参数*checkSessionKey需检查的在Session中保存的关键字*redirectURL如果用户未登录,则重定向到指定的页面,URL不包括ContextPath*notCheckURLList不做检查的URL列表,以分号分开,并且URL中不包括ContextPath*/
5、publicclassCheckLoginFilterimplementsFilter{protectedFilterConfigfilterConfig=null;privateStringredirectURL=null;privateListnotCheckURLList=newArrayList();privateStringsessionKey=null;publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,Filter
6、ChainfilterChain)throwsIOException,ServletException{HttpServletRequestrequest=(HttpServletRequest)servletRequest;HttpServletResponseresponse=(HttpServletResponse)servletResponse;HttpSessionsession=request.getSession();if(sessionKey==null){filterChain.doFilter(request,r
7、esponse);return;}if((!checkRequestURIIntNotFilterList(request))&&session.getAttribute(sessionKey)==null){response.sendRedirect(request.getContextPath()+redirectURL);return;}filterChain.doFilter(servletRequest,servletResponse);}publicvoiddestroy(){notCheckURLList.clear(
8、);}privatebooleancheckRequestURIIntNotFilterList(HttpServletRequestrequest){Stringuri=request.getServletPath()+(reque
此文档下载收益归作者所有