单用户登陆,踢出前一个同名用户

来源:互联网 发布:江湖家居源码 编辑:程序博客网 时间:2024/06/09 10:11

*******在web.xm中做session过期时间设置************
<session-config>
  <session-timeout>15</session-timeout>
 </session-config>
 

******************LogonAction.java******************************
ActionServlet servlet = this.getServlet();         
HttpSession session = request.getSession();
ServletContext servletContext = servlet.getServletContext();
HttpSession oldSession = (HttpSession) servletContext.getAttribute(loginId);
boolean isFirstLogin = (oldSession == null);
boolean isSameLogin = (oldSession != null) && oldSession.getId().equals(session.getId());      
if (isFirstLogin || !isSameLogin) {
             servletContext.setAttribute(loginId, session);
}

******************在其他需要验证的action中加入一下代码*****************
ActionServlet servlet = this.getServlet();
ServletContext servletContext = servlet.getServletContext();
HttpSession oldSession = (HttpSession) servletContext.getAttribute(loginId);    
if(session.getAttribute(Constants.CURRENT_USER)==null){
   session.setAttribute("message", "timeout");
   return mapping.findForward("timeoutfail");
}
if (oldSession != null&&!(oldSession.getId().equals(session.getId())))  {  
       request.getSession().invalidate();
      return (mapping.findForward("failoldsession"));
 }
    

 

原创粉丝点击