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
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Reply 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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Invalidate session when browser is closed
 
Similar Threads
How Handel Browser Event in Javascript?
Session handling
Session
How to invaliddate session when browser Window is closed using [x] button
How To Handel The Browser Close Event in Servlets?