有用的过滤器.doc_第1页
有用的过滤器.doc_第2页
有用的过滤器.doc_第3页
有用的过滤器.doc_第4页
有用的过滤器.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

有用的过滤器一、使浏览器不缓存页面的过滤器 import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ForceNoCacheFilter implements Filter public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException (HttpServletResponse) response).setHeader(Cache-Control,no-cache); (HttpServletResponse) response).setHeader(Pragma,no-cache); (HttpServletResponse) response).setDateHeader (Expires, -1); filterChain.doFilter(request, response); public void destroy() public void init(FilterConfig filterConfig) throws ServletException 二、检测用户是否登陆的过滤器 import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.List; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.IOException; public class CheckLoginFilter implements Filter protected FilterConfig filterConfig = null; private String redirectURL = null; private List notCheckURLList = new ArrayList(); private String sessionKey = null; public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession(); if(sessionKey = null) filterChain.doFilter(request, response); return; if(!checkRequestURIIntNotFilterList(request) & session.getAttribute(sessionKey) = null) response.sendRedirect(request.getContextPath() + redirectURL); return; filterChain.doFilter(servletRequest, servletResponse); public void destroy() notCheckURLList.clear(); private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) String uri = request.getServletPath() + (request.getPathInfo() = null ? : request.getPathInfo(); return notCheckURLList.contains(uri); public void init(FilterConfig filterConfig) throws ServletException this.filterConfig = filterConfig; redirectURL = filterConfig.getInitParameter(redirectURL); sessionKey = filterConfig.getInitParameter(checkSessionKey); String notCheckURLListStr = filterConfig.getInitParameter(notCheckURLList); if(notCheckURLListStr != null) StringTokenizer st = new StringTokenizer(notCheckURLListStr, ;); notCheckURLList.clear(); while(st.hasMoreTokens() notCheckURLList.add(st.nextToken(); java源动力,java开源社区, 三、字符编码的过滤器 import javax.servlet.*; import java.io.IOException; public class CharacterEncodingFilter implements Filter protected FilterConfig filterConfig = null; protected String encoding = ; public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException if(encoding != null) servletRequest.setCharacterEncoding(encoding); filterChain.doFilter(servletRequest, servletResponse); public void destroy() filterConfig = null; encoding = null; public void init(FilterConfig filterConfig) throws ServletException this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter(encoding); 四、资源保护过滤器 package catalog.view.util; import javax.servlet.Filter; import javax.servlet.FilterConfig; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.Iterator; import java.util.Set; import java.util.HashSet;/ import mons.logging.Log; import mons.logging.LogFactory; public class SecurityFilter implements Filter /the login page uri private static final String LOGIN_PAGE_URI = login.jsf; /the logger object private Log logger = LogFactory.getLog(this.getClass(); /a set of restricted resources private Set restrictedResources; public void init(FilterConfig filterConfig) throws ServletException this.restrictedResources = new HashSet(); this.restrictedResources.add(/createProduct.jsf); this.restrictedResources.add(/editProduct.jsf); this.restrictedResources.add(/productList.jsf); public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException this.logger.debug(doFilter); String contextPath = (HttpServletRequest)req).getContextPath(); String requestUri = (HttpServletRequest)req).getRequestURI(); this.logger.debug(contextPath = + contextPath); this.logger.debug(requestUri = + requestUri); if (this.contains(requestUri, contextPath) & !this.authorize(HttpServletRequest)req) this.log

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论