使用filter使session失效的用户,…

使用filter使session失效的用户,重新跳转到登录页面: 
1.前台简单的登录测试页面login.jsp 
Java代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path request.getContextPath();  
  4. String basePath request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/" 
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>" 
  11.       
  12.     <title>My JSP 'login.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache" 
  15.     <meta http-equiv="cache-control" content="no-cache" 
  16.     <meta http-equiv="expires" content="0"     
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" 
  18.     <meta http-equiv="description" content="This is my page" 
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css" 
  21.     -->  
  22.     <script type="text/javascript" 
  23.        function submitForm(){  
  24.           document.getElementByIdx_x_x_x_x_x("form1").submit();   
  25.         
  26.     </script>  
  27.   
  28.   </head>  
  29.     
  30.   <body>  
  31.     This is Login page. <br>  
  32.     <form action="login" method="post" id="form1" name="form1" 
  33.        UserName:<input type="text" id="userName" name="userName"/><input type="button" value="submit" οnclick="submitForm()" id="submit1" />  
  34.     </form>  
  35.   </body>  
  36. </html>  


2.struts.xml的配置信息: 
Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC    
  3.      "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"    
  4.     "http://struts.apache.org/dtds/struts-2.1.dtd" 
  5. <struts>  
  6.     <package name="default" extends="struts-default" namespace="/" 
  7.         <action name="login" class="com.wl.action.test.LoginAction" 
  8.             <result name="success" 
  9.                /success.jsp  
  10.             </result>  
  11.         </action>  
  12.     </package 
  13. </struts>  


3.LoginAction如下: 
Java代码 
  1. package com.wl.action.test;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import com.opensymphony.xwork2.ActionContext;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. public class LoginAction extends ActionSupport  
  9.   
  10.     String userName;  
  11.       
  12.     @Override  
  13.     public String execute() throws Exception  
  14.           
  15.         ActionContext context=ActionContext.getContext();  
  16.         Map session=context.getSession();  
  17.         System.out.println("userName="+userName);  
  18.         session.put("userName"userName);  
  19.         return SUCCESS;  
  20.      
  21.   
  22.     public String getUserName()  
  23.         return userName;  
  24.      
  25.   
  26.     public void setUserName(String userName)  
  27.         this.userName userName;  
  28.      
  29.  


4.过滤器FilterTest如 下: 
Java代码 
  1. package com.wl.filter.test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.Filter;  
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.FilterConfig;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import javax.servlet.http.HttpSession;  
  14.   
  15. public class FilterTest implements Filter  
  16.   
  17.     public void destroy()  
  18.         // TODO Auto-generated method stub  
  19.   
  20.      
  21.   
  22.     public void doFilter(ServletRequest req, ServletResponse res,  
  23.             FilterChain chain) throws IOException, ServletException  
  24.         // TODO Auto-generated method stub  
  25.   
  26.         HttpServletRequest httpReq=(HttpServletRequest)req;  
  27.         HttpServletResponse httpRes=(HttpServletResponse)res;  
  28.         HttpSession httpSession=httpReq.getSession();  
  29.         if(httpSession.getAttribute("userName")==null){  
  30.             httpRes.sendRedirect("../login.jsp");  
  31.         }else 
  32.             chain.doFilter(req, res);  
  33.          
  34.      
  35.   
  36.     public void init(FilterConfig arg0) throws ServletException  
  37.         // TODO Auto-generated method stub  
  38.   
  39.      
  40.   
  41.  


5.配置Web.xml信息: 
添加信息: 
Java代码 
  1. <!-- configure filter -->  
  2.   <filter>  
  3.      <filter-name>filterTest</filter-name>  
  4.      <filter-class>com.wl.filter.test.FilterTest</filter-class 
  5.   </filter>  
  6.   <filter-mapping>  
  7.      <filter-name>filterTest</filter-name>  
  8.      <url-pattern>/filterJsp/*</url-pattern>  
  9.   </filter-mapping>  
  10.   <!-- configure session timeout one minute -->  
  11.   <session-config>  
  12.       <session-timeout>1</session-timeout>  
  13.   </session-config>  


6.成功跳转页面success.jsp如下: 
Java代码 
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <%  
  4. String path request.getContextPath();  
  5. String basePath request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/" 
  6. %>  
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>" 
  12.       
  13.     <title>My JSP 'success.jsp' starting page</title>  
  14.       
  15.     <meta http-equiv="pragma" content="no-cache" 
  16.     <meta http-equiv="cache-control" content="no-cache" 
  17.     <meta http-equiv="expires" content="0"     
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3" 
  19.     <meta http-equiv="description" content="This is my page" 
  20.     <!--  
  21.     <link rel="stylesheet" type="text/css" href="styles.css" 
  22.     -->  
  23.   
  24.   </head>  
  25.     
  26.   <body>  
  27.     Success. <br>  
  28.     <a href="filterJsp/ExtremeCompomentTest_1.jsp">Forward to Filter URL</a>  
  29.   </body>  
  30. </html>  


7.配置了一个Session的监听器来监听Session是否失效 
Java代码 
  1. package com.wl.listener.test;  
  2.   
  3. import javax.servlet.http.HttpSessionEvent;  
  4. import javax.servlet.http.HttpSessionListener;  
  5.   
  6. public class HttpSessionListenerTest implements HttpSessionListener  
  7.   
  8.     public void sessionCreated(HttpSessionEvent arg0)  
  9.         // TODO Auto-generated method stub  
  10.   
  11.         System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS);  
  12.      
  13.   
  14.     public void sessionDestroyed(HttpSessionEvent arg0)  
  15.         // TODO Auto-generated method stub  
  16.   
  17.         System.out.println("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE");  
  18.      
  19.   
  20.  


8.WebRoot的目录结构: 
----WebRoot 
       ------filterJsp 
                -----ExtremeCompomentTest_1.jsp 
       ------login.jsp 
       ------success.jsp 

9.结果: 
在IE中输入:http://localhost:8080/FileUpload/login.jsp如下显示 
使用filter使session失效的用户,重新跳转到登录页面(转)  
提交表单之后跳转的页面为: 

使用filter使session失效的用户,重新跳转到登录页面(转)  
等待1分钟之后,在Eclipse的控制台出现"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"信息时,即Session已经失效了,再点击上面的"Forward to Filter URL"链接,这时候过滤器filter就会起作用,验证Session失效后,跳转到登录界面。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值