检测session是否为空
Posted 来自IT国的猴子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测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=‘/index.jsp?error=timeout‘;");
out.print("</script>");
}
}
@Override
public void destroy() {
}
}
过滤*.do的文件
以上是关于检测session是否为空的主要内容,如果未能解决你的问题,请参考以下文章