• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Logout user upon browser close

 
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot reliably detect the browser closing.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic