• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logout user upon browser close  RSS feed

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that implements HttpSessionListener. It has methods

sessionCreated ( HtttpSessionEvent event) and
sessionDestroyed ( HttpSessionEvent event)

Now what is happening is when I login none of these methods is called and when I close browser or click logout or move to a new webpage then sessionCreated is called but not sessionDestroyed where I am doing my stuff when user logs out.

Thanks
Imad
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another important thing is that my sessionCreated(HttpSessionEvent event) method is called as soon as I visit my page using http://localhost/myApp and right now i m just on the login screen

and since sessionCreated has been called thyen when I log in sessionCreated is not called.
 
Shu Gao
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, you can configure a <session-config> element
<session-config>
<session-timeout>1</session-timeout>
</session-config>
in your web.xml, so that the session will expire after a period of time. (in mins)

When the user closes the browser, the servlet-api will check on the session, when a session is checked on, it will be created, that's why the sessionCreated() method is called. And I guess in your code, this code never appears as well:
request.getSession(true);
whereas you are manually checking on a session and telling the API even if the session doesn't exists so far, create it anyway specified by the true flag.

So, if you have configured your session timeout, then after you closed your browser, when the specified time expires, say 1 minute, then after a minute, the sessionDestroyed() method will be called.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot reliably detect the browser closing.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!