jsp防止重复登录问题以及关闭浏览器,意外断电等情况使用户退出的解决方法

一、防止用户重复登录

这是登录请求界面submitLogin.jsp,只有java代码:

 

[java]  view plain copy
  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 plain copy
  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 plain copy
  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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值