views:

2210

answers:

2

I am writing an application in GWT and I need to detect when a user navigates away from my application or when he closes the browser window (onUnload event) and do a logout (session invalidation and few other cleanup tasks). The logout action is performed by a servlet.

I am currently doing this by hooking into the onUnload() event and opening a new window pointed to the logout servlet.

Is there a better way to do this? Any other suggestions are welcome.

+3  A: 

Why not just make a very short lived session cookie that is reset with each page load, then add a tracking cookie. When the user returns you notice the tracking cookie but no session cookie. Expire the session and clear everything up at that point.

Pop up blockers will prevent your session clean up when it blocks the onUnload window open, because this is something spammers use.

apphacker
+4  A: 

Looks like GWT does have an event for exactly this.

ClosingEvent.

Looks like you need to implement a ClosingHandler

Carnell
You should be able to send one last logout request to your server in your handler.
Carnell
I just ran a quick test to create a ClosingHandler and registered it. I made a simple one that just displays an alert. The code is getting fired when the tab/window is closed as well as when I browse to another page outside of the app.
Carnell
I am aware of this WindowCloseListener (equivalent of ClosingEvent in GWT 1.5), but was in the assumption that since a RequestCallback object is mandatory to make a Request, it may cause some problem on the callback events. But I tried with a RequestCallback doing nothing on the onResponseReceived event, and it is working fine.Thanks for the response.
sarav