aspose file tools*
The moose likes Struts and the fly likes Invalidate session when browser is closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Invalidate session when browser is closed" Watch "Invalidate session when browser is closed" New topic
Author

Invalidate session when browser is closed

Jordan Smith
Ranch Hand

Joined: Apr 06, 2008
Posts: 89
hi

i would like to invalidate the session when the browser/tab is closed by the user.

i thought that maybe using ajax will make it but i don't know how.

how can i do it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

You'll need to send an AJAX request to the server using the onunload event of the body. Look on google to find a good tutorial on AJAX like this or this or this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That probably wouldn't work if the browser crashes, but I'm not sure. You could also send a periodic request from every page (like once a minute or something) and if that "keepalive" request doesn't come in the session is invalidated.
Jordan Smith
Ranch Hand

Joined: Apr 06, 2008
Posts: 89
how do i send a periodic request from every page?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Start a periodic timer, and when it fires, make an ajax request to find out if the session has expired.

The app would need to keep track of sessions to make sure each session gets its respective periodic request.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

odelya yomtov wrote:
i would like to invalidate the session when the browser/tab is closed by the user.


Unfortunately,there is no way for a server to detect the event.
 
jQuery in Action, 2nd edition
 
subject: Invalidate session when browser is closed