Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to detect if user close the browser

Status
Not open for further replies.

yytan

Programmer
May 8, 2002
113
0
0
MY
hi all;
how to code in servlet, if the user close browser, then the server will know and invoke some command, let say disconnect from db???
 
You cannot do it in the servlet, but you can do it in javascript, and then call a servlet with the news ...

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
  function closeDown() {
   alert('closing browser');
   document.location = &quot;/servlet/MyServlet?useraction=logout&quot;;
  }
</script>
</head>
<body onUnload=&quot;closeDown()&quot;>
Hello
</body>
</html>
 
hi;

i think my explanation wasnt clear.

1. i create a db conn in session upon user login succesful.
2. if the session die, it will invoke method to disconnect from the db. &quot;implements HttpSessionBindingListener&quot;
3. if user click logout, it will also invoke method to disconnect from the db.

but, wat if user close the browser, how do i close that db connection.
 
As I said earlier, because servlets and JDBC code run on the server, they do not know when the browser is closed. But JavaScript runs on the browser, and so can detect browser closing events.

So you catch those events with the code I supplied above, and then call a servlet to clean up anything - such as closing db conns.
 
First you should use DataSources so that you always put your connection back in a pool after a request.

And You should never keep a connection in a session between two requests!!
 
One problem you might run into is that the JavaScript onUnload runs everytime you leave a page. So when a user closes the browser, clicks on a link, clicks back, clicks forward, submits a form or for any reason leaves the current page to go to another, onUnload is called first and then the link. Because you have redefined what happens when a page is unloaded (the user is redirected to another page) this pretty much means users will not be able to do anything on your site.

They will go to your page and then click on a link. Whenever they take an action will then be navigated to &quot;/servlet/MyServlet?useraction=logout&quot;. The link to the other page is now forgotten. Oops.

There is one small but complex and annoying way around this. It is similar to URL rewriting. Every link on the page is run through another JavaScript function that kills the onUnload and then redirects the user properly.

Try this code:

<html>
<script language=&quot;javascript&quot;>
function navigate( linkURL ) {
onunload = &quot;&quot;;
document.location = linkURL;
}
</script>
<body onunload=&quot;document.location='<a href=&quot;javascript:navigate(' I want to go to Google</a>
</body>
</html>

OnUnload takes the user to Yahoo. Open this page and then type another address into the address bar. Notice you cannot navigate to any other page other than yahoo. Now click on the link. Now I let you go.

Now you still have problems with handling the forward and backward buttons. Good luck. You can use window.location.forward(); to always push to user to the most recent page in history. But be sure to test is to be sure it gives you the result you want.
 
Dear Kindon;

glad to hear from u after, such a long period nobody has reply on this topic.

well, i am still thinking of the easiest and fatest way to solve my prob.

thanks for ur tips. if i have no more idea, i will adopt ur suggest to curb my prob.

rgd / yokeyew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top