aspose file tools*
The moose likes Java in General and the fly likes destroying session when user closes browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "destroying session when user closes browser" Watch "destroying session when user closes browser" New topic
Author

destroying session when user closes browser

Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Hi! everybody,

I would like to clear session when user closes browser window by clicking
the "x" in upper right hand corner. Since this is a client side event, how
do I run server code when user clicks "x"?

Please give me some idea.

Regards

Jolly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

The body element has an attribute on..unload, you can call some javascript method over here which will pass the control to your controller class.

A more elegant way is to have sessionlisteners that will hear to all open sessions, as soon as a session is abruptly ended you can check with your sessionlistener class and implement your logic accordingly. This basically will be kind of a maintenance activity.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

What you want is not really possible.

HTTP is a stateless, connectionless protocol. Also, browsers do not send a message to the server when they close. So it is impossible for the server to know that a user has closed his/her browser.

What is usually done in web applications is that there's a timeout on the session in the server. If a user doesn't use the session for a certain amount of time (for example, 15 or 30 minutes, or whatever you think is appropriate), the server automatically discards the session.

Note that this is the standard way of doing things in Java EE: In the deployment descriptor (web.xml) of your webapp you can set the session timeout in the web-app / session-config / session-timeout element.

The onunload about which Anand talks is only called when you switch from one page to another; not when the browser is closed.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Jesper Young:
What you want is not really possible.

HTTP is a stateless, connectionless protocol. Also, browsers do not send a message to the server when they close. So it is impossible for the server to know that a user has closed his/her browser.

What is usually done in web applications is that there's a timeout on the session in the server. If a user doesn't use the session for a certain amount of time (for example, 15 or 30 minutes, or whatever you think is appropriate), the server automatically discards the session.

Note that this is the standard way of doing things in Java EE: In the deployment descriptor (web.xml) of your webapp you can set the session timeout in the web-app / session-config / session-timeout element.

The onunload about which Anand talks is only called when you switch from one page to another; not when the browser is closed.


Most of what you say is correct, but, not all.

Indeed onunload is called when when you switch from one page to another but, don't you think that closong of the window is similar to migrating away from that page.
Well, onunload is always called whenever you migrate away from a page and you can very well check if that was window kill operation. This can be very well be used to solve the current issue in which even after the window is closed the javascript function that is called on onunload will submit the control to some controller class which can continiye processing even after the browser is closed, condition that we keep the information abut every current session in some variable. Indeed, in this case the response will be not rendered anywhere so one can avoid any response rendering, just pojo's.

Moreover, things might be difficult to implement but, might not be infeasible. Similar is the case with this one.

That's why I suggested the more elegant way of using session listeners which will keep track of all active sessions and hereby assist to destroy a session when it is no longer active.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Anubhav Anand:
Indeed onunload is called when when you switch from one page to another but, don't you think that closong of the window is similar to migrating away from that page.


*I* would think that, yes. When I tried some years ago, there was at least one version of Internet Explorer that didn't agree.


Well, onunload is always called whenever you migrate away from a page and you can very well check if that was window kill operation.


Which browsers have you tried this with?

That's why I suggested the more elegant way of using session listeners which will keep track of all active sessions and hereby assist to destroy a session when it is no longer active.


I don't see how a session listener can be used to initiate the invalidation of a session, as it will only be notified when a session already is about to be invalidated.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Which browsers have you tried this with?

I once used the mentioned feature of "Onunload" in one of my projects. We tested that for IE6,7 and Mozilla Firefox2(all versions/updates) and Netscape Navigator. It worked fine with them.

I don't see how a session listener can be used to initiate the invalidation of a session, as it will only be notified when a session already is about to be invalidated.

What I meant with sessionlisteners is that at any point of time we can get the list of active sessions. Now, when a browser is closed then we can always keep some property that will be set to false for that session. Now, in my application my controller class can talk with the my session listener and check if some session is no longer active e.g. if property realted to closing of browser is false. Then, I can very well remove the data for the session. So, as I mentioned in my first post this will be a kind of maintenance activity.

Also, another method can be to have a custom controller whose job is to just clear session. This can be called with the help of "Onunload" or some other way to clear the data for the Invalidated session which in our case is the session which is no longer active because of clicking on "X".
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Hi! Again,

Let me elaborate my problem . the thing i want to do is that i have to set a flag in the database for the user who is going to log out and in the current scenario i have to handle that also ,if he/she clicks on "X" in the browser window then also flag should be set in the database.

As Anand has said we can use onUnload() method but in that case if the user clicks on some link on that page,i.e migrating from one page to another, then also this onUnload() method will be called.

please correct me ,if i am wrong.

Regards

Jolly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

As Anand has said we can use onUnload() method but in that case if the user clicks on some link on that page,i.e migrating from one page to another, then also this onUnload() method will be called.


Onunload is not a method but an attribut of the <body> tag of HTML.Precisely


Now, mySpecificMethod method will be called only when user closes the browser or refreshes the page.
Also, this will depend on the way you implement your mySpecificMethod method.
A bit of googling for javascript methods for browser events may solve your problem.
[ March 10, 2008: Message edited by: Anubhav Anand ]
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Instead of closing the browser window,If the user clicks on any of the links provided on the page,in that case also the method specified against onUnload attribute gets invoked which in turn invokes the clean up task for that user while the user is still there.

I hope i am able to express the problem clearly

Regards

Jolly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Try something like this:

Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
first of all Thanks for the nice idea..
things are working now but now the problem is how to invoke the server code .If i go for the servlet approach ,i mean i am going to write that clean up task inside servlet then no doubt it can be invoked but as the browser window is closed at the time of invocation there is nothing on the client side to receive the output respose and an exception is thrown on the server.

So there should be something which doesn't send the reponse back.

Please suggest something for this ..

Regards

Jolly
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Originally posted by Jolly tiw:
first of all Thanks for the nice idea..
things are working now but now the problem is how to invoke the server code .If i go for the servlet approach ,i mean i am going to write that clean up task inside servlet then no doubt it can be invoked but as the browser window is closed at the time of invocation there is nothing on the client side to receive the output respose and an exception is thrown on the server.

So there should be something which doesn't send the reponse back.

Please suggest something for this ..

Regards

Jolly



Does it really matter. I mean simple POJO's nothing more than cleaning unwanted data. Once you are done a kind of simple redirect, which obviously won't work as nothing to redirect. Does this actually give a server side error ?

Did you try that ?
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
yeah buddy it works.
thanks for your help.
Actually exception is raised when the servlet couldn't find the same browser instance opened.i mean if the browser which invoked the request is closed and we opened another browser from the same java script code after requesting the servlet and closing the current browser then we find exception on the server console.


Thanks & Regards

Jolly
 
 
subject: destroying session when user closes browser