• 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

How to invalidate the session on browser close.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
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
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,.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
It's feeding time! Give me the food you were going to give to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic