首页 > 代码库 > 检测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是否为空
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。