码迷,mamicode.com
首页 > Web开发 > 详细

struts2、jsp的简单路径的简单拦截

时间:2016-11-06 07:43:00      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:com   eth   struts2   http   name   comment   设置   blog   null   

  <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
        
    }   
  

  
}  

 

 

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

技术分享

 

struts2、jsp的简单路径的简单拦截

标签:com   eth   struts2   http   name   comment   设置   blog   null   

原文地址:http://www.cnblogs.com/tk55/p/6034632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!