In my web app, when a user logs in, I add his Id to a vector of valid Ids in the servlet, when he logs out, I remove his Id from the vector, so I can see how many current us
Here is a dirty way to accomplish "detecting a browser close" I found this in a forum somewhere on this site, when I find it again I will link to it.
Add additional headers to the http response. When additional requests come in they will contain these additional headers. You can check for these headers and if they aren't present you can kill the session, so a new one will start. While this doesn't "notify" you if the browser is closed it can tell you if someone has closed their window and is attempting to come back, since the headers you added will be missing.
wrapper.setHeader("Cache-Control", "no-cache, no-store, must- revalidate");
wrapper.setHeader("Pragma", "no-cache");
wrapper.setDateHeader("Expires", 0);
Where wrapper is a response wrapper.
if (wrapper.getHeaderNames().size() < the number of headers you are expecting)
request.getSession().invalidate();
Not perfect but it might help someone.