首页 > 代码库 > 检测session是否为空

检测session是否为空

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.toher.filter;

import cn.toher.bean.User;
import java.io.IOException;
import java.io.PrintWriter;
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;

/**
 *
 * 
 * @date 2015-4-28 15:30:52
 */
public class LoginFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        
    }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;
        HttpSession session = servletRequest.getSession();
        User user = (User)session.getAttribute("Suser");
// 过滤session是不是为空,为空则remove一下,然后跳到登录界面
if(user!=null){ chain.doFilter(request, response); }else{ session.removeAttribute("Suser"); PrintWriter out = servletResponse.getWriter(); out.print("<script language=‘javascript‘>"); out.print("top.location.href=http://www.mamicode.com/‘/index.jsp?error=timeout‘;"); out.print("</script>"); } } @Override public void destroy() { } }

过滤*.do的文件

 

检测session是否为空