jsp防止重複登錄問題以及關閉瀏覽器,意外斷電等情況使用戶退出的解決方法

一、防止用戶重複登錄

這是登錄請求界面submitLogin.jsp,只有java代碼:

 

[java] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@page import="com.jdbc.entity.UserInfoEntity"%>  
  3. <%@page import="com.jdbc.manager.SchoolManager"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8.   
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  10. <%   
  11. request.setCharacterEncoding("UTF-8");  
  12. UserInfoEntity user=new UserInfoEntity();  
  13. String logname=request.getParameter("logname");  
  14. String userpwd=request.getParameter("userpwd");  
  15. user.setLogname(logname);  
  16. user.setKeyword(userpwd);  
  17. //獲取在線用戶的集合  
  18. List<UserInfoEntity> onlineList=(List<UserInfoEntity>)application.getAttribute("ol");  
  19. if(onlineList==null){  
  20. onlineList=new ArrayList<UserInfoEntity>();  
  21. application.setAttribute("ol",onlineList);  
  22. }  
  23. //判斷用戶是否已登錄 已登錄的話返回登錄界面。    
  24. for(int i=0;i<onlineList.size();i++){  
  25. if(onlineList.get(i).getLogname().equals(user.getLogname())){  
  26. response.sendRedirect("Login.jsp?add=-1");  
  27. return;  
  28. }  
  29. }  
  30. //調用登錄驗證方法,判斷用戶是否再存   
  31. user=SchoolManager.checkLogin(user);  
  32. //用戶存在 將該用戶添加到在線列表onlineList中,給該用戶一個session並且進入主界面    
  33. if(user!=null){  
  34. onlineList.add(user);  
  35. session.setAttribute("user",user);  
  36. response.sendRedirect("../Index/index.jsp");  
  37. }else{  
  38. response.sendRedirect("Login.jsp?add=0");  
  39. }  
  40.   
  41. %>  


 

二、用戶退出請求界面submitLogin.jsp
[java] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%@page import="com.jdbc.entity.UserInfoEntity"%>  
  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 'submitExits.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.    UserInfoEntity user=(UserInfoEntity)session.getAttribute("user");  
  27.    List<UserInfoEntity> onlineList=(List<UserInfoEntity>)application.getAttribute("ol");  
  28.    for(int i=0;i<onlineList.size();i++){  
  29.       if(onlineList.get(i).getLogname().equals(user.getLogname())){  
  30.       
  31.     onlineList.remove(i);  
  32.     session.invalidate();  
  33.    }  
  34.    }  
  35.      
  36.    %>  
  37.   <body>  
  38.      
  39.   </body>  
  40.   <script type="text/javascript">  
  41.   window.close();  
  42.   </script>  
  43. </html>  


問題來了,若用戶關閉瀏覽器或意外斷電等情況發生時時,那麼在用戶useronlineList中還存在,他再次登錄的時候會提示已在線該如何解決?

 

 

三、關閉瀏覽器時用戶的退出 

老師說出現這種情況的話要重建寫一界面,利用框架解決,具體我還沒有搞清楚,等弄明白了在把這塊更新上。

 

 

四、意外斷電等情況時的用戶退出:

這個就要利用HttpSessionListener來解決了,首先建一個共具類SystemWebListener實現接口HttpSessionListener。

然後在web.xml中進行如下配置:
  <listener>
   <listener-class>com.jdbc.tool.SystemWebListener</listener-class>//這個必須是你這個工具類的路徑 不要寫錯
  </listener>

工具類代碼如下:

 

[java] view plaincopy
  1. package com.jdbc.tool;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6. import javax.servlet.ServletContext;  
  7. import javax.servlet.http.HttpSession;  
  8. import javax.servlet.http.HttpSessionEvent;  
  9. import javax.servlet.http.HttpSessionListener;  
  10.   
  11. import com.jdbc.entity.UserInfoEntity;  
  12.   
  13. public class SystemWebListener implements HttpSessionListener{  
  14.   
  15.     public void sessionCreated(HttpSessionEvent se) {  
  16.           
  17.     }  
  18.   
  19.     public void sessionDestroyed(HttpSessionEvent se) {  
  20.         try {  
  21.             HttpSession session=se.getSession();  
  22.             ServletContext app=session.getServletContext();  
  23.             List<UserInfoEntity> useronlineList=(List<UserInfoEntity>) app.getAttribute("ol");  
  24.             UserInfoEntity user=(UserInfoEntity) session.getAttribute("user");  
  25.             for (int i = 0; i < useronlineList.size(); i++) {  
  26.                 if(useronlineList.get(i).getLogname().equals(user.getLogname())){  
  27.                     useronlineList.remove(i);  
  28.                     return;  
  29.                 }  
  30.                   
  31.             }  
  32.         } catch (Exception e) {  
  33.             // TODO: handle exception  
  34.         }  
  35.     }  
  36.   
  37. }  
轉自sdd379733766
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章