使用过滤器来检测session失效

来源:互联网 发布:在mac上安装jmeter 编辑:程序博客网 时间:2024/04/30 01:25
 public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {


        if (debug) {
            log("SessionTimeoutFilter:doFilter()");
        }
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        doBeforeProcessing(request, response);


        HttpSession session = req.getSession();
        //用户超时或没有登陆时跳转到登陆页面  
        logger.debug("before doFilter****");
        String uri = req.getRequestURI();
        if (uri.indexOf("sessiontimeout")==-1&&uri.indexOf("SysConfig")==-1&&(session.getAttribute("UserName") == null || session.isNew())) {
            resp.sendRedirect(req.getContextPath()+"/sessiontimeout.jsp");
            //resp.sendRedirect(req.getContextPath()+"/sessiontimeout.jsp"); 不能使用相对路径,不同的页面过来,相对路径会不同
            return;
        }
        Throwable problem = null;
        try {
            chain.doFilter(request, response);
        } catch (Throwable t) {
            // If an exception is thrown somewhere down the filter chain,
            // we still want to execute our after processing, and then
            // rethrow the problem after that.
            problem = t;
            t.printStackTrace();
        }
        logger.debug("after doFilter****");
        doAfterProcessing(request, response);


        // If there was a problem, we want to rethrow it if it is
        // a known type, otherwise log it.
        if (problem != null) {
            if (problem instanceof ServletException) {
                throw (ServletException) problem;
            }
            if (problem instanceof IOException) {
                throw (IOException) problem;
            }
            sendProcessingError(problem, response);
        }
    }
0 0
原创粉丝点击