欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Filter解决session 过期,跳转到登陆页面

程序员文章站 2024-03-20 18:16:28
...

环境:DWR+ Struts

配置:

1、过滤.DO请求,在web.xml中加入

 

<web-app>
  <filter>
  	<filter-name>session-timeout</filter-name>
  	<filter-class>com.manage.common.SessionFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>session-timeout</filter-name>
  	<url-pattern>*.do</url-pattern>
  </filter-mapping>

 2、写过滤器Filter类

 

public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest requestHttp = (HttpServletRequest) request;
		String requestURI = requestHttp.getRequestURI().toLowerCase();
		// 判断是否是首次登陆
		boolean isLogin = requestURI.indexOf("login") >= 0;
		Tsysuser tsysuser = (Tsysuser) requestHttp.getSession().getAttribute("tsysuser");
		if (!isLogin && tsysuser == null) {
			request.setAttribute("message", "登陆超时,请重新登陆!");
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.jsp");
			requestDispatcher.forward(request, response);
		}else{
			chain.doFilter(request, response);
		}

	}

 

3、在登陆页面index.jsp的<head></head>标签中加入

 

<script language="JavaScript"> 
	if (window != top) 
	top.location.href = location.href; 
</script>