javaweb 同名用户登录剔除退出功能

原理:每一个用户,登录前有一个验证,当第一次登录时,会把其session信息,添加到一个特定的静态变量中。当第二次登录时,验证到静态变量中存在该用户的信息,就表示为重复登录。

jsp代码,一个form表单提交:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <form action="/struts2upload/system/login/reLogin.action"  
  2.             method="post">  
  3.             用户名:  
  4.             <input type="text" id="txtUser" name="txtUser" value="" />  
  5.             密  码:  
  6.             <input type="text" id="txtPass" name="txtPass" value="" />  
  7.             <input type="submit" id="subOk" value="确定" />  
  8.         </form>  
struts2配置:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <action name="reLogin" class="userLoginAction" method="reLogin">  
  2.             <result name="input" type="redirect">/relogin.jsp</result>  
  3.             <result name="success" type="redirect">/ok.jsp</result>  
  4.         </action>  
action代码:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.      * 可判断用户是否重复登录 
  3.      */  
  4.     public String reLogin() {  
  5.         String userId = this.getTxtUser();//ServletActionContext.getRequest().getParameter("txtUser");  
  6.         UserInfo user = new UserInfo();  
  7.         user.setUserId(userId);  
  8.         //验证该用户ID,是否已经登录。当前用户比较已登录到系统的静态变量中的值,是否存在。  
  9.         Boolean hasLogin = SessionUserListener.checkIfHasLogin(user);  
  10.         // 如果重复登录,控制端则打印信息,返回登录页面  
  11.         if (hasLogin) {  
  12.             System.out.println(user.getUserId()+"已经登录到本系统。");  
  13.             return "input";  
  14.             // SessionUserListener.removeUserSession(userId);  
  15.         } else {  
  16.             // 如果没有重复登录,则将该登录的用户信息添加入session中  
  17.             ServletActionContext.getRequest().getSession().setAttribute("userInfo", user);  
  18.             // 比较保存所有用户session的静态变量中,是否含有当前session的键值映射,如果含有就删除  
  19.             if (SessionUserListener.containsKey(ServletActionContext.getRequest().getSession().getId())) {  
  20.                 SessionUserListener.removeSession(ServletActionContext.getRequest().getSession().getId());  
  21.             }  
  22.             //把当前用户封装的session按,sessionID和session进行键值封装,添加到静态变量map中。  
  23.             SessionUserListener.addUserSession(ServletActionContext.getRequest().getSession());  
  24.         }  
  25.         return "success";  
  26.     }  
session监听类:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.zyujie.listener;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.http.HttpSession;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11. import javax.servlet.http.HttpSessionListener;  
  12.   
  13. import com.zyujie.pojo.UserInfo;  
  14.   
  15. public class SessionUserListener implements HttpSessionListener {  
  16.   
  17.     // key为sessionId,value为HttpSession,使用static,定义静态变量,使之程序运行时,一直存在内存中。  
  18.     private static java.util.Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>(500);  
  19.   
  20.     /** 
  21.      * HttpSessionListener中的方法,在创建session 
  22.      */  
  23.     public void sessionCreated(HttpSessionEvent event) {  
  24.         // TODO Auto-generated method stub  
  25.     }  
  26.   
  27.     /** 
  28.      * HttpSessionListener中的方法,回收session时,删除sessionMap中对应的session 
  29.      */  
  30.     public void sessionDestroyed(HttpSessionEvent event) {  
  31.         getSessionMap().remove(event.getSession().getId());  
  32.     }  
  33.   
  34.     /** 
  35.      * 得到在线用户会话集合 
  36.      */  
  37.     public static List<HttpSession> getUserSessions() {  
  38.         List<HttpSession> list = new ArrayList<HttpSession>();  
  39.         Iterator<String> iterator = getSessionMapKeySetIt();  
  40.         while (iterator.hasNext()) {  
  41.             String key = iterator.next();  
  42.             HttpSession session = getSessionMap().get(key);  
  43.             list.add(session);  
  44.         }  
  45.         return list;  
  46.     }  
  47.   
  48.     /** 
  49.      * 得到用户对应会话map,key为用户ID,value为会话ID 
  50.      */  
  51.     public static Map<String, String> getUserSessionMap() {  
  52.         Map<String, String> map = new HashMap<String, String>();  
  53.         Iterator<String> iter = getSessionMapKeySetIt();  
  54.         while (iter.hasNext()) {  
  55.             String sessionId = iter.next();  
  56.             HttpSession session = getSessionMap().get(sessionId);  
  57.             UserInfo user = (UserInfo) session.getAttribute("userInfo");  
  58.             if (user != null) {  
  59.                 map.put(user.getUserId(), sessionId);  
  60.             }  
  61.         }  
  62.         return map;  
  63.     }  
  64.   
  65.     /** 
  66.      * 移除用户Session 
  67.      */  
  68.     public synchronized static void removeUserSession(String userId) {  
  69.         Map<String, String> userSessionMap = getUserSessionMap();  
  70.         if (userSessionMap.containsKey(userId)) {  
  71.             String sessionId = userSessionMap.get(userId);  
  72.             getSessionMap().get(sessionId).invalidate();  
  73.             getSessionMap().remove(sessionId);  
  74.         }  
  75.     }  
  76.   
  77.     /** 
  78.      * 增加用户到session集合中 
  79.      */  
  80.     public static void addUserSession(HttpSession session) {  
  81.         getSessionMap().put(session.getId(), session);  
  82.     }  
  83.   
  84.     /** 
  85.      * 移除一个session 
  86.      */  
  87.     public static void removeSession(String sessionID) {  
  88.         getSessionMap().remove(sessionID);  
  89.     }  
  90.   
  91.     public static boolean containsKey(String key) {  
  92.         return getSessionMap().containsKey(key);  
  93.     }  
  94.   
  95.     /** 
  96.      * 判断该用户是否已重复登录,使用 
  97.      * 同步方法,只允许一个线程进入,才好验证是否重复登录 
  98.      * @param user 
  99.      * @return 
  100.      */  
  101.     public synchronized static boolean checkIfHasLogin(UserInfo user) {  
  102.         Iterator<String> iter = getSessionMapKeySetIt();  
  103.         while (iter.hasNext()) {  
  104.             String sessionId = iter.next();  
  105.             HttpSession session = getSessionMap().get(sessionId);  
  106.             UserInfo sessionuser = (UserInfo) session.getAttribute("userInfo");  
  107.             if (sessionuser != null) {  
  108.                 if (sessionuser.getUserId().equals(user.getUserId())){  
  109.                     return true;  
  110.                 }  
  111.             }  
  112.         }  
  113.         return false;  
  114.     }  
  115.   
  116.     /** 
  117.      * 获取在线的sessionMap 
  118.      */  
  119.     public static Map<String, HttpSession> getSessionMap() {  
  120.         return sessionMap;  
  121.     }  
  122.   
  123.     /** 
  124.      * 获取在线sessionMap中的SessionId 
  125.      */  
  126.     public static Iterator<String> getSessionMapKeySetIt() {  
  127.         return getSessionMap().keySet().iterator();  
  128.     }  
  129. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值