通過JS執行關閉視窗時session.invalidate()方法

來源:互聯網
上載者:User

 HttpSessionListener挺有用,用它統計當前線上的使用者的人數。使用者開啟一個視窗時,系統將調用HttpSessionListener的sessionCreated方法,在此方法裡邊將登陸使用者增加到SESSION中,在使用者關閉BROWER時,調用HttpSessionListener的sessionDestroyed方法,將使用者從SESSION中刪除,

但是當我關閉BROWER時,HttpSessionListener的sessionDestroyed方法並沒有調用.

HttpSessionListener的sessionDestroyed方法只有兩個時候才被調用,第一個時候是SESSION逾時的時候會調用,第二個時候手動調用session.invalidate()方法時會調用.

 

為瞭解決在關閉視窗時刪除session,可以通過js

 

在瀏覽器關閉的時候open出一個隱藏的視窗來登出session,登出後再close這個頁面  
  <script>  
  function   window.onbeforeunload()  
  {  
      if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)  
      {  
            window.open('out.jsp','','height=100,width=100,top=5000,left=5000,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,   status=no')  
      }  
  }  
  </script>  
   
   
  out.jsp  
  ----------------------------  
  <%  
  //這裡登出session  
  %>  
  <script>  
   
  window.close();  
  </script>

 

因為現在很多攔截快顯視窗的外掛程式,所有可以用一個xmlhttp  

 

//用xmlhttp去登出,不開新視窗  
  <script>  
  function   window.onbeforeunload()  
  {  
      if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)  
      {  
              var   xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");  
              xmlhttp.open("GET",   "out.jsp",   false);  
              xmlhttp.send();      
      }  
  }  
  </script>

 

這個方法確實可以實現刪除原來的session.

不過卻發現同時會產生另一個session.不過這個可以在自己的代碼裡面自己判斷一下實現他。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.