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 "/servlet/MyServlet?useraction=logout". 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="javascript">
function navigate( linkURL ) {
onunload = "";
document.location = linkURL;
}
</script>
<body onunload="document.location='
<a href="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.