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
in JavaScript you can use the onbeforeclose event to pass a call back to the server when the user closes the browser.
onbeforeclose
I typically use a synchronous Ajax call to do this.