We help IT Professionals succeed at work.

how to catch the event when browser closes

gordon_guan
gordon_guan asked
on
Medium Priority
241 Views
Last Modified: 2008-03-17
I have a applet embedded in HTML file, now I need to catch the event when user closes the browser. How this can be done in Java?
Comment
Watch Question

CERTIFIED EXPERT
Commented:
The applet's stop() method should be called when the user leaves the page containing the applet, and
the applet's destroy() method will be called when the applet is about to be shutdown.
CERTIFIED EXPERT

Commented:
Unfortunately, "destroy" is not necessarily called when the browser quits.  Last I checked, neither Netscape nor IE invoked the applet "destroy" on exit.

You can try adding a shutdown hook to the VM if the applet "destroy" is never called.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Why do you want to know when the browser closes?

Author

Commented:
Then the question is how do I know when stop() or destroy() is called? Do I need to rewrite these methods in order to do necessary clean up work when user leaves the applet?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Exactly.
The browser will then call these methods when it deems appropriate.

Author

Commented:
I think I should make it more clear: my applet will show another page using showDocument() accoring to normal procedure, for which I don't need to do any clean up work. but if the user directly close the browser, this is another issue, i need to do some clean work after that. So I need to distinguish these two situations. So if I use stop or destroy, even for my normal procedure, the method will also be executed, since the applet any way comes to its stop already, any suggestions?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I'd possibly rethink your design.
There's no way of knowing for sure when the browser is closed, and your applet may not even be running when it occurs.

What sort of clean up do you need to do when the user closes the browser? And why can't you simply do it when the applet is shutdown?

Author

Commented:
I have solved this problem, I just use a flag in my overwriting method of stop(), which will call super.stop(), and here, I will check the flag(the flag will be set to 1 if the process is going correctly before showDocument() is executed, if the user directly close the browser, flag will remains 0), after checking, I can distinguish these two situations and do my clean up work.
Many thanks to all of you.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
super.stop() is actually empty so u don't need to call it.
Still unclear when you clean up after someone has gone to another page but as long as your happy :)

Author

Commented:
Yup, special thanks to you, objects.
concerning the super methods, it's not really my concern. as long as stop() will be called when the user closes the browser and when user leaved for next page. That's good enough for me. :)
Thanks a lot
CERTIFIED EXPERT

Commented:
Cool discussion ! I slept during it...
In my opinion objects deserves the points :-)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
no you were 1st with the required information :)

Author

Commented:
hehe, both of you deserve the points, but I can only give it to one person, maybe next time both of you can help me again, then I can give the points to objects... :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.