使用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失效後,跳轉到登錄界面。 
發佈了29 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章