File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Invalidating a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Invalidating a session" Watch "Invalidating a session" New topic

Invalidating a session

Kris Parr

Joined: Aug 13, 2001
Posts: 8
I need to invalidate a session when the browser window is closed. I've looked at using the onUnload event but this doesn't seem to be able to submit a form to a servlet. Using a JSP Method seems to execute the method on compilation and not wait for the event to trigger. Any ideas on how I can achieve this ?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
There is no guaranteed method for doing exactly what you want. You could put a short lifetime on the session and use some sort of "heartbeat" applet or Javascript on the page to keep it alive.

author of:
DC Dalton
Ranch Hand

Joined: May 28, 2001
Posts: 287
I just went thru this same problem building a who's on type program. You can use the onUnload event BUT it will trigger everytime the user changes pages. I actually had a script working that ran them thru a logout servlet with the onUnload event (Javascript) but it turned out to be a major headache so I scrapped it & we went with a ServletBindingListener class & set the session.setMaxInactiveInterval() method so that if no session activity was detected for 15 minutes the valueUnbound method was called & wrote to the database to log the user out. Its really not as hard as you would think, tough I must admit I sought outside help for it. What you are trying to do with Javascript is next to impossible because your JavaSCript object exist on the clients machine, hence when the browser is closed the object dies....You can do just about anything with a ServletBindingListener class because no matter what the user does (wander off the site, close the browser, etc) The valueUnbound method of your listener still exists & the object itself will exist until it does what you want it to because it resides on the server.........hope this helps
I agree. Here's the link:
subject: Invalidating a session
It's not a secret anymore!