JSP/Servlet == server side. Browser events == client side. So no. The only thing a Servlet (and hence a JSP) in its typical context is going to know about is an HTTP request. Since closing the browser doesn't make any request to the server, not message is passed to the Servlet. You need JavaScript for browser events.
Not all ways in which a browser window can be closed will result in a Javascript event being fired.
If there were you could in a Javascript event handler do a request to the server which would launch some cleanup code or whatever, but this isn't reliable (as we found out, one of our marketing people had thought up a requirement to do exactly that, we had to tell him it wasn't possible and to train the users to use the logout button instead).
There is no way to detect when the browser window is closed...onunload is the closest you are going to get to it.
Just a few points to ponder as you develop this application: You need to consider that the onunload gets called when the page is closed, user navigates away by link or typing in the url, and refreshing the page. So if you need to distinguish between these, then you will have some difficulty.
Also the onunload event will not fire if user gets disconnected from internet, user's browser freezes, or my favorite reason "If JavaScript is disabled by the user"
That is why I really on my session timing out on the server to clean up the mess.
Eric Pascarello
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop