本帖最后由 王震阳 于 2014-5-13 21:59 编辑
通常情况下,java web项目都是通过过滤器来判断session是否失效。下面做了一个例子,实现过滤器验证用户的会话是否丢失。
web.xml中的配置:
- <filter>
- <filter-name>filter</filter-name>
- <filter-class>com.zyujie
- .common.LoginFilter</filter-class>
- </filter>
- <!-- servlet规范,不能以/*.jsp这样的结尾,写全,或者写成/app/*,这样的才行 -->
- <filter-mapping>
- <filter-name>filter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <session-config>
- <session-timeout>1</session-timeout>
- </session-config>
复制代码
过滤器类:
- package com.itheima.common;
- 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;
- public class LoginFilter implements Filter {
- public void destroy() {
- // TODO Auto-generated method stub
- }
- public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
- // TODO Auto-generated method stub
- HttpServletRequest request = (HttpServletRequest) req;
- HttpServletResponse response = (HttpServletResponse) res;
- HttpSession session = request.getSession();
- // 如果session不为空,则可以浏览其他页面
- String url = request.getServletPath();
- System.out.println(url);
- //这里判断目录,后缀名,当然也可以写在web.xml中,用url-pattern进行拦截映射
- if ((!request.getServletPath().equals("/login.action"))
- && (!request.getServletPath().equals("/login.jsp"))
- && (!request.getServletPath().equals("/relogin.jsp"))
- && (!request.getServletPath().equals("/jquery-1.8.0.min.js"))) {
- System.out.println(request.getServletPath());
- if (session.getAttribute("userInfo") == null) {
- session.invalidate();
- response.setContentType("text/html;charset=gb2312");
- PrintWriter out = response.getWriter();
- out.println("<script language='javascript' type='text/javascript'>");
- out.println("alert('由于你长时间没有操作,导致Session失效!请你重新登录!');window.location.href='" + request.getContextPath() + "/relogin.jsp'");
- out.println("</script>");
- } else {
- chain.doFilter(request, response);
- }
- } else {
- chain.doFilter(request, response);
- }
- }
- public void init(FilterConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
- }
- }
复制代码 【阳哥专栏】点点滴滴记录最真实的黑马
http://bbs.itheima.com/thread-117773-1-1.html
(出处: 黑马程序员IT技术论坛)
|
|