判断是否重复登录
当用户登录系统时,需要进行是否重复登录的判断,这时候需要用到监听器Listener,具体使用如下:
构建一个类SessionUserListener,具体实例如下:
package net.nk.struts.listener;/** * @author xiaoye * @version create time:Oct 15, 2010 11:19:02 AM * 类说明 */public class SessionUserListener implements HttpSessionListener {//key为sessionId,value为HttpSessionprivate static java.util.Map<String, HttpSession> sessionMap = new java.util.concurrent.ConcurrentHashMap<String, HttpSession>(500);protected final Logger logger = Logger.getLogger(SessionUserListener.class);/*** HttpSessionListener中的方法,在创建session*/public void sessionCreated(HttpSessionEvent event) {}/*** HttpSessionListener中的方法,回收session时,删除sessionMap中对应的session*/public void sessionDestroyed(HttpSessionEvent event) { getSessionMap().remove(event.getSession().getId());}/***得到在线用户会话集合*/public static List<HttpSession> getUserSessions(){List<HttpSession> list = new ArrayList<HttpSession>();Iterator<String> iterator = getSessionMapKeySetIt();while(iterator.hasNext()){String key = iterator.next();HttpSession session = getSessionMap().get(key);list.add(session);}return list;}/***得到用户对应会话map,key为用户ID,value为会话ID*///public static Map<String, String> getUserSessionMap(){Map<String, String> map = new HashMap<String, String>();Iterator<String> iter = getSessionMapKeySetIt();while(iter.hasNext()){String sessionId = iter.next();HttpSession session = getSessionMap().get(sessionId);SmUser user = (SmUser)session.getAttribute(Parameters.SESSION_USER);if(user!=null){map.put(user.getUserId(), sessionId);}}return map;}/***移除用户Session*/public synchronized static void removeUserSession(String userId){Map<String, String> userSessionMap = getUserSessionMap();if(userSessionMap.containsKey(userId)){ String sessionId = userSessionMap.get(userId); getSessionMap().get(sessionId).invalidate(); getSessionMap().remove(sessionId);}}/***增加用户到session集合中*/public static void addUserSession(HttpSession seccion){getSessionMap().put(seccion.getId(), seccion);}/***移除一个session*/public static void removeSession(String sessionID){getSessionMap().remove(sessionID);}public static boolean containsKey(String key){return getSessionMap().containsKey(key);}/** * 判断该用户是否已重复登录 * @param user * @return */public synchronized static boolean checkIfHasLogin(SmUser user){Iterator<String> iter = getSessionMapKeySetIt();while(iter.hasNext()){String sessionId = iter.next();HttpSession session = getSessionMap().get(sessionId);SmUser sessionuser = (SmUser)session.getAttribute(Parameters.SESSION_USER);if(sessionuser!=null){if(sessionuser.getUserId().equals(user.getUserId())) return true;}}return false;}/***获取在线的sessionMap*/public static Map<String, HttpSession> getSessionMap() {return sessionMap;}/***获取在线sessionMap中的SessionId*/public static Iterator<String> getSessionMapKeySetIt() {return getSessionMap().keySet().iterator();}}
?
?在web.xml中配置一行以下代码加载该监听器:
<listener><listener-class>net.nk.struts.listener.SessionUserListener</listener-class></listener>
?在登录的action中进行判断该用户是否重复登录了,示例代码如下:
Boolean hasLogin = SessionUserListener.checkIfHashLogin(smUser);//如果重复登录了,则注销之前已登录的用户if(hasLogin){ SessionUserListener.removeUserSession(userId);}else{ //如果没有重复登录,则将该登录的用户信息添加入session中 request.getSession.setAttribute("userInfo",smUser); //创建session if(SessionUserListener.containsKey(request.getSession().getId())){ SessionUserListener.removeSession(request.getSession().getId());} SessionUserListener.addUserSession(request.getSession());}
?
?
在退出系统的时候,需要注意将该session失效,示例如下:
request.getSession().setAttribute("userInfo",null);request.getSession().invalidate();
?
?