做的一个b/s的应用程序,要求一个用户在某一时间点只能在一个地方登录,现在SessionSingleBean里判断全局Session,但是如果用户点X来关闭页面,Session需要清除,在网上找到的方法:
<body  onunload="unload();">
<script type="text/javascript">
 function unload()     
  {   
         
        if(event.clientX<=0 && event.clientY<0)   
        {   
    
     ;//处理关闭事件
             
        }
 }</script>
在处理关闭事件处直接写java代码没有执行,想通过跳转到exit.jsp页面里再处理Session清除等
用 1、top.location=...;2、windows.open();3、window.showModalDialog();这三种方法都没有办法跳转到exit.jsp页面,
不知道有没有人遇到过这个问题,如何解决呢?

解决方案 »

  1.   

    可以使用session事件监听, 另外, 楼主给的js代码中, 那个位置是让你写ajax请求的...
      

  2.   

    浏览器端获取关闭浏览器时间发送异步请求的方式来清除不怎么可靠哦。万一脚本出了错,或是浏览器崩了,就不行了。
    我用的土办法是在webservice中注册一个定时器,定时器中维护一个列表(单例),列表存放若干“登录者”对象,登录者对象中包含账号、ip和最后操作时间,帐户登录时则写入一个,帐户操作时则更新最后操作时间,登出则删除一个,若重复登陆则判断容器中是否有此对象,无则可,有则判断ip(此三个红字方法无需同步,因为单一账号不存在并发请求的现象,也就是不会并发的删除、修改列表容器中的同一个对象),定时器则定时执行一些任务,遍历列表容器中所有“登录者”对象,发现有最后操作时间超过一定时常的就删去(此处操作可能与上述三个操作之一发生并发,会抛出异常,需要捕获异常并再次尝试,当尝试超过五次还是十次后仍旧不行则可以放弃此次操作)
      

  3.   

    关于Session事件监听的实现,不知道有人做过吗?
      

  4.   

    关于Session事件监听的实现,不知道有人做过吗?
      

  5.   

    关于Session事件监听的实现,不知道有人做过吗?
      

  6.   


    function unload() {          
       if(event.clientX <=0 && event.clientY <0) {      
            <%=session.removeAttribute("LoginUser")%>   //从session中删除用户.
        } 

      

  7.   

    修改一下:function unload() {          
      if(event.clientX <=0 && event.clientY <0) {      
            <%=session.invalidate()%>  //清空session.
        } 

      

  8.   

    我来接分。不过另外的一种不能用HIBERNATE去操作数据库,只能用JDBC package com.jzaccp.oa.grbg.sessionlistener; import javax.servlet.http.*; 
    import javax.servlet.*; 
    import java.util.*; public class onLineUser implements HttpSessionBindingListener { 
        public onLineUser(){ 
      }   private Vector users=new Vector(); 
      /** 
        * 获取在线人数 
        * @return 
        */ 
      public int getCount(){ 
            users.trimToSize(); 
          return users.capacity(); 
      } 
      /** 
        * 判断用户是否存在 
        * @param userName 
        * @return 
        */ 
      public boolean existUser(String userName){    
      users.trimToSize(); 
          boolean existUser=false; 
          for (int i=0;i <users.capacity();i++ ) 
          { 
              if (userName.equals((String)users.get(i))) 
              { 
                  existUser=true; 
                  break; 
              } 
          } 
          return existUser; 
      } 
    /** 
    * 删除用户 
    * @param userName 
    * @return 
    */ 
      public boolean deleteUser(String userName) { 
          users.trimToSize(); 
          if(existUser(userName)){ 
              int currUserIndex=-1; 
              for(int i=0;i <users.capacity();i++){ 
                  if(userName.equals((String)users.get(i))){ 
                      currUserIndex=i; 
                      break; 
                  } 
              } 
              if (currUserIndex!=-1){ 
                  users.remove(currUserIndex); 
                  users.trimToSize(); 
                  return true; 
              } 
          } 
          return false; 
      } 
    /** 
    * 得到用户的列表 
    */ 
      public Vector getOnLineUser() 
      { 
          return users; 
      } 
      /** 
        * 植入session时调用的方法 
        * 
        */ 
        public void valueBound(HttpSessionBindingEvent e) { 
          users.trimToSize(); 
          if(!existUser(e.getName())){ 
              users.add(e.getName()); 
              System.out.print(e.getName()+"\t  登入到系统\t"+(new Date())); 
              System.out.println("      在线用户数为:"+getCount()); 
          }else 
              System.out.println(e.getName()+"已经存在"); 
        } 
    /** 

    * session过期或被关闭或退出调用的方法 
    */ 
        public void valueUnbound(HttpSessionBindingEvent e) { 
            users.trimToSize(); 
            String userName=e.getName(); 
            deleteUser(userName); 
            System.out.print(userName+"\t  退出系统\t"+(new Date())); 
            System.out.println("      在线用户数为:"+getCount()); 
          } } 
      

  9.   

    我来接分。不过另外的一种不能用HIBERNATE去操作数据库,只能用JDBC package com.jzaccp.oa.grbg.sessionlistener; import javax.servlet.http.*; 
    import javax.servlet.*; 
    import java.util.*; public class onLineUser implements HttpSessionBindingListener { 
        public onLineUser(){ 
      }   private Vector users=new Vector(); 
      /** 
        * 获取在线人数 
        * @return 
        */ 
      public int getCount(){ 
            users.trimToSize(); 
          return users.capacity(); 
      } 
      /** 
        * 判断用户是否存在 
        * @param userName 
        * @return 
        */ 
      public boolean existUser(String userName){    
      users.trimToSize(); 
          boolean existUser=false; 
          for (int i=0;i <users.capacity();i++ ) 
          { 
              if (userName.equals((String)users.get(i))) 
              { 
                  existUser=true; 
                  break; 
              } 
          } 
          return existUser; 
      } 
    /** 
    * 删除用户 
    * @param userName 
    * @return 
    */ 
      public boolean deleteUser(String userName) { 
          users.trimToSize(); 
          if(existUser(userName)){ 
              int currUserIndex=-1; 
              for(int i=0;i <users.capacity();i++){ 
                  if(userName.equals((String)users.get(i))){ 
                      currUserIndex=i; 
                      break; 
                  } 
              } 
              if (currUserIndex!=-1){ 
                  users.remove(currUserIndex); 
                  users.trimToSize(); 
                  return true; 
              } 
          } 
          return false; 
      } 
    /** 
    * 得到用户的列表 
    */ 
      public Vector getOnLineUser() 
      { 
          return users; 
      } 
      /** 
        * 植入session时调用的方法 
        * 
        */ 
        public void valueBound(HttpSessionBindingEvent e) { 
          users.trimToSize(); 
          if(!existUser(e.getName())){ 
              users.add(e.getName()); 
              System.out.print(e.getName()+"\t  登入到系统\t"+(new Date())); 
              System.out.println("      在线用户数为:"+getCount()); 
          }else 
              System.out.println(e.getName()+"已经存在"); 
        } 
    /** 

    * session过期或被关闭或退出调用的方法 
    */ 
        public void valueUnbound(HttpSessionBindingEvent e) { 
            users.trimToSize(); 
            String userName=e.getName(); 
            deleteUser(userName); 
            System.out.print(userName+"\t  退出系统\t"+(new Date())); 
            System.out.println("      在线用户数为:"+getCount()); 
          } } 
      

  10.   

    我来接分。不过另外的一种不能用HIBERNATE去操作数据库,只能用JDBC package com.jzaccp.oa.grbg.sessionlistener; import javax.servlet.http.*; 
    import javax.servlet.*; 
    import java.util.*; public class onLineUser implements HttpSessionBindingListener { 
        public onLineUser(){ 
      }   private Vector users=new Vector(); 
      /** 
        * 获取在线人数 
        * @return 
        */ 
      public int getCount(){ 
            users.trimToSize(); 
          return users.capacity(); 
      } 
      /** 
        * 判断用户是否存在 
        * @param userName 
        * @return 
        */ 
      public boolean existUser(String userName){    
      users.trimToSize(); 
          boolean existUser=false; 
          for (int i=0;i <users.capacity();i++ ) 
          { 
              if (userName.equals((String)users.get(i))) 
              { 
                  existUser=true; 
                  break; 
              } 
          } 
          return existUser; 
      } 
    /** 
    * 删除用户 
    * @param userName 
    * @return 
    */ 
      public boolean deleteUser(String userName) { 
          users.trimToSize(); 
          if(existUser(userName)){ 
              int currUserIndex=-1; 
              for(int i=0;i <users.capacity();i++){ 
                  if(userName.equals((String)users.get(i))){ 
                      currUserIndex=i; 
                      break; 
                  } 
              } 
              if (currUserIndex!=-1){ 
                  users.remove(currUserIndex); 
                  users.trimToSize(); 
                  return true; 
              } 
          } 
          return false; 
      } 
    /** 
    * 得到用户的列表 
    */ 
      public Vector getOnLineUser() 
      { 
          return users; 
      } 
      /** 
        * 植入session时调用的方法 
        * 
        */ 
        public void valueBound(HttpSessionBindingEvent e) { 
          users.trimToSize(); 
          if(!existUser(e.getName())){ 
              users.add(e.getName()); 
              System.out.print(e.getName()+"\t  登入到系统\t"+(new Date())); 
              System.out.println("      在线用户数为:"+getCount()); 
          }else 
              System.out.println(e.getName()+"已经存在"); 
        } 
    /** 

    * session过期或被关闭或退出调用的方法 
    */ 
        public void valueUnbound(HttpSessionBindingEvent e) { 
            users.trimToSize(); 
            String userName=e.getName(); 
            deleteUser(userName); 
            System.out.print(userName+"\t  退出系统\t"+(new Date())); 
            System.out.println("      在线用户数为:"+getCount()); 
          } }