首页 >  JAVA频道 > 实训项目 > 

java web项目防止多用户重复登录解决方案(2)

java web项目防止多用户重复登录解决方案(2)

作者:yjl 来源:华育国际 时间:2015-02-27 访问次数:3637
登录入口处理完之后,考虑到回话结束的话,那么对应的登录用户也应该相应的注销登录。我们可以写一个Session监听器,监听sessioon销毁的时候,我们将登录的用户注销掉,也就是从application中移除。表示该用户已经下线了

登录入口处理完之后,考虑到回话结束的话,那么对应的登录用户也应该相应的注销登录。我们可以写一个Session监听器,监听sessioon销毁的时候,我们将登录的用户注销掉,也就是从application中移除。表示该用户已经下线了。
代码如下:

public class SessionListener implements HttpSessionListener,ServletContextListener{
    
    private int count;
    private ServletContext servletContext = null;
        
    public SessionListener() {
        count = 0;
    }
    
    private Logger logger = Logger.getLogger(this.getClass());
    @Override
    public void sessionCreated(HttpSessionEvent event) {
        count++;
        setContext(event);
        logger.info("***************the  http session is created...***************");
    }
    
    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        //在session销毁的时候 把loginUserMap中保存的键值对清除  
        User user = (User)event.getSession().getAttribute("loginUser");
        if(user!=null){
            MaploginUserMap = (Map)event.getSession().getServletContext().getAttribute("loginUserMap");
            loginUserMap.remove(user.getFuUserName());
            event.getSession().getServletContext().setAttribute("loginUserMap",loginUserMap);
        }
            
        count--;
        setContext(event);
        logger.info("***************the  http session is destroyed...***************");
    }
    
    public void setContext(HttpSessionEvent httpSessionEvent){
        httpSessionEvent.getSession().getServletContext().setAttribute("online", count);
    }
        
        
    @Override
    public void contextDestroyed(ServletContextEvent servletcontextevent) {      
        this.servletContext = null;
        logger.info("***************the  servlet context is destroyed...***************");
    }
    
    @Override
    public void contextInitialized(ServletContextEvent servletcontextevent) {
        this.servletContext = servletcontextevent.getServletContext();
        logger.info("***************the  servlet context is initialized...***************");
    }
public class SessionListener implements HttpSessionListener,ServletContextListener{
private int count;
private ServletContext servletContext = null;
public SessionListener() {
count = 0;
}
private Logger logger = Logger.getLogger(this.getClass());
@Override
public void sessionCreated(HttpSessionEvent event) {
count++;
setContext(event);
logger.info("***************the  http session is created...***************");
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
//在session销毁的时候 把loginUserMap中保存的键值对清除
User user = (User)event.getSession().getAttribute("loginUser");
if(user!=null){
MaploginUserMap = (Map)event.getSession().getServletContext().getAttribute("loginUserMap");
loginUserMap.remove(user.getFuUserName());
event.getSession().getServletContext().setAttribute("loginUserMap",loginUserMap);
}
count--;
setContext(event);
logger.info("***************the  http session is destroyed...***************");
}
public void setContext(HttpSessionEvent httpSessionEvent){
httpSessionEvent.getSession().getServletContext().setAttribute("online", count);
}
@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
this.servletContext = null;
logger.info("***************the  servlet context is destroyed...***************");
}
@Override
public void contextInitialized(ServletContextEvent servletcontextevent) {
this.servletContext = servletcontextevent.getServletContext();
logger.info("***************the  servlet context is initialized...***************");
}
web.xml中配置如下:
[html] view plaincopyprint?

   com.facelook.util.SessionListener

com.facelook.util.SessionListener