jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes How do i know when browser is closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do i know when browser is closed" Watch "How do i know when browser is closed" New topic

How do i know when browser is closed

Rehana Shaik
Ranch Hand

Joined: Jan 03, 2003
Posts: 33
how would i know when user closes the browser.because i have to roll back some of the transactions when user closes the browser. from html i can use onunload method .but i want to know browser closing event at server side.At the same time how can i know particular session is inactive? our session timeout period is about 15 minutes so i can't wait for session timeout and rollback.
Thanks in advance
[ March 08, 2004: Message edited by: Rehana Shaik ]
john smith
Ranch Hand

Joined: Mar 04, 2004
Posts: 75
Relying on browser events using JavaScript is inherently unsafe - I'd recommend you don't use it. Rather than trying to roll back a transaction when the browser is closed, include some confirmation action to commit the whole transaction once the user has done whatever you need them to do - if it is never reached, then the transaction is never committed.
You will only know if the session is inactive if it has timedout. Reduce the size of your session timeout.
[ March 08, 2004: Message edited by: cluck cluck ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

"Cluck Cluck"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
Thanks! and welcome to the JavaRanch!
Good Answer to the question though.

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Leaving a transaction open and waiting for user intervention is usually a bad idea. You might want to re-think what you're doing. If you can (if it's not a lot of data), you might want to gather up everything you need to perform the transaction and store it in the session. Then, at the end, you use that information to perform the entire transaction during one request. If the user closes their browser or their session times out, the data for the transaction is lost. You could actually save the data in the database (marking it as temporary or provisional somehow) and listen for session timeout events (via an HttpSessionListener) to clean it up. However you do it, I would NOT leave open a JDBC connection to the database while waiting for the user to do something. That can be quite a resource hog, especially if your session-timeout value is large. Just my $0.02.

James Carman, President<br />Carman Consulting, Inc.
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
At the same time how can i know particular session is inactive?

Add a class implementing HttpSessionListener interface in your application. this interface has 2 methods -
1. sessionCreated
2. sessionDestroyed
when your session gets inactive, sessionDestroyed() method would be called to indicate the same.
Dont forget to register this class as listener in your web.xml.
I hope, this helps. :-)
I agree. Here's the link: http://aspose.com/file-tools
subject: How do i know when browser is closed
It's not a secret anymore!