I am trying to clear the HttpSession if the consumer close the browser window. I dont know how to do it, Please help me anyone
Thanks & Regards Chakri
if you close the browser it is already clear the session
but if you want to force to clear the session
usesession.invalidate();
or if you want to remove specific session use session.removeAttribute("<sessionName>");
EDITED :
try reading the answer on this Page
"HTTP is not a continuous-conversation protocol where the client connects to the server and they trade information asynchronously until the client disconnects.
Instead, each HTTP client request acts like it logs in, does one thing, then logs out. This request/response cycle repeats until the client stops sending requests.
At NO time is the server allowed to send a response to the client unless it's in response to a specific actual request and only one response is permitted per request.
So you cannot have a server post a "you timed out" page to the client. The closest you can get is to respond with a "you were timed out" response page if/when the client makes a request.
The other common thing people want to do is to notify the server that the client has closed a browser, window, or tab. There is no protocol defined for the World Wide Web to deal with that. If the user closes a server window/tab, the server is not notified by the client. Since there is no ongoing session to be dropped (remember, the connection only lasts long enough for the server to accept a request and return a response), there's no way the server will know that the client went away."
SUMMARIZATION :
YOU CAN'T DO IT
Possible Workaround :
You simply need to rely upon the session timeout. The timeout could happen at any time and you cannot even know if the user is even looking at one of your pages when it occurs. It's likely they're off looking at videos of kittens at the time.
Short answer: you can't. Fortunately, sessions expire automatically after a period of time; check your servlet engine documentation to see how to set this timeout period.
I felt that there should be a way to do it from JavaScript; unfortunately, JavaScript doesn't seem to have an onWindowClose handler.
It can be archived in a little diffrent way.
use a javascript event known as "window.onbeforeunload"
e.g window.onbeforeunload=mylogic_function(); function mylogic_function() { window.location.href = "path to the servlet which inavalidate the session"; }
Try this ..hope this will work
If you could get the browser to (reliably) notify the server that the user had closed the window, then the server could call session.invalidate()
as per the original Answer provided by ejay_francisco.
The difficulty is getting the notification to happen reliably. There are various ways to detect the window close; e.g. as covered by these Questions (and similar):
You could then write the (javascript) close event handler to send a specific request to the server.
However, I don't think any scheme is going to be able to deal with cases where the user's browser dies, the user's machine is shut down, and similar scenarios. So if you need the session to be cleared 100% of the time, then you are probably out of luck. I don't think it can be done.