• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to invalidate the session on browser close.

 
Selvakumar Kumarasamy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can any one help me in invalidating the session when the user is closing the browser. ie. whenever the user closes the browser the session should be automatically invalidated.

Thanks in advance.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't possible. A Session in HTTP is simply a bookmark that the server sends to the client so that on the next request the same application context will be used. Either side can discard this bookmark and the session is effectively ended, although in practice when a J(2)EE server invalidates a session, it also releases the session objects for potential garbage collection.

HTTP isn't a protocol that relies on a continuous communication like client/server does. It's strictly request/response and effectively each request/response transaction stands by itself, which is why the "bookmark" concept was developed to maintain the illusion of a formal connect/converse/disconnect mode of operation. Since the actual connect/disconnect occurs once per request, there's no way for the server to be notified that the browser is no longer communicating. If the browser wants to do another request, that's fine, but if the browse never submits another request again, that's also fine.

Because of this, J2EE supports the idea that HttpSession objects have a countdown timer whose duration is defined in the web.xml file. If no requests are made within the countdown interval, the server will consider that as an indication that the user has gone away and discard the HttpSession. To force logout, you invoke the session.invalidate() method.
 
Pedro Erencia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could detect when browser is closed and then perform an AJAX request to the server. The question is how that detection can be done on a cross-browser manner ..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, good luck with that. Or you could just do what every reputable web developer does and let the session time-out on its own,.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pedro Erencia:
You could detect when browser is closed and then perform an AJAX request to the server. The question is how that detection can be done on a cross-browser manner ..

Hint: It can't. Don't waste your time going down this road.

Selvakumar: While the session isn't invalidated, it can't be used anymore either. The user is effectively disconnected from the session when the browser is closed. So nobody will access it until it expires and then it goes away on its own.
 
Pedro Erencia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:

Hint: It can't. Don't waste your time going down this road.


If it can be done or not depends on the requeriments. How many browsers are required to be supported ? Is a world wide accesible application or an intranet app which would be accessed from a particular and identificable machine ? ( i've done applications like that ).

Browser close is detectable.



Selvakumar: While the session isn't invalidated, it can't be used anymore either. The user is effectively disconnected from the session when the browser is closed. So nobody will access it until it expires and then it goes away on its own.


Not to be paranoid but it can occur
 
Paul Clapham
Sheriff
Posts: 21872
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pedro Erencia:
Browser close is detectable.
And the next question: will browsers let you send an HTTP request after you detect the closing? The example linked to only pops up a Javascript alert.
 
Pedro Erencia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
And the next question: will browsers let you send an HTTP request after you detect the closing? The example linked to only pops up a Javascript alert.


I've not done the test but if it lets you do an alert, why not an AJAX request ?
[ August 28, 2008: Message edited by: Pedro Erencia ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!