zoukankan      html  css  js  c++  java
  • struts2、jsp的简单路径的简单拦截

      <filter>    
      <filter-name>UsersFilter</filter-name>    
      <filter-class>com.web.UsersFilter</filter-class>    
     </filter>    
     <filter-mapping>    
      <filter-name>UsersFilter</filter-name>    
      <url-pattern>/*</url-pattern>    
     </filter-mapping> 
    package com.web;
    import java.io.IOException;   
      
    import javax.servlet.Filter;   
    import javax.servlet.FilterChain;   
    import javax.servlet.FilterConfig;   
    import javax.servlet.ServletException;   
    import javax.servlet.ServletRequest;   
    import javax.servlet.ServletResponse;   
    import javax.servlet.http.HttpServletRequest;   
    import javax.servlet.http.HttpServletResponse;   
    import javax.servlet.http.HttpSession;   
    public class UsersFilter implements Filter {   
        public void doFilter(ServletRequest request, ServletResponse response,   
                FilterChain chain) throws IOException, ServletException {   
            // 设置请求的字符编码   
            request.setCharacterEncoding("UTF-8");   
            // 设置返回请求的字符编码   
            response.setCharacterEncoding("UTF-8");   
            // 转换ServletRequest为 HttpServletRequest   
            HttpServletRequest req = (HttpServletRequest) request;   
            // 转换ServletResponse为HttpServletRequest   
            HttpServletResponse res = (HttpServletResponse) response;   
            // 获取Session   
            HttpSession session = req.getSession();   
            // 获取Session中存储的对象   
            Object o = session.getAttribute("user");   
            // 获取当前请求的URI   
            String url = req.getRequestURI();   
            // 判断Session中的对象是否为空;判断请求的URI是否为不允许过滤的URI   
            if (o==null && url.indexOf("test.jsp")<0
                    && url.indexOf("comment.jsp")<0
                    && url.indexOf("link.jsp")<0) {   
                chain.doFilter(request, response);   
                res.setHeader("Cache-Control","no-store");         
                res.setDateHeader("Expires",0);      
                res.setHeader("Pragma","no-cache");    
                res.flushBuffer(); 
                } 
        }
    
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
    
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
            
        }   
      
    
      
    }  

    ****************************************************************************************

  • 相关阅读:
    Linux忘了root的密码怎么办
    缩略图的实现
    ASP.NET程序编写注意 (转载)
    太极拳
    Linux系统管理技巧大荟萃
    茶经(转载)
    datagrid的显示控制
    太极功
    Linux下硬盘分区详解
    Tomcat4.0中文问题简单解决方法
  • 原文地址:https://www.cnblogs.com/tk55/p/6034632.html
Copyright © 2011-2022 走看看