In this case I think you will need to go back to the first option, of using Javascript to capture the close event and then either an AJAX request or a form submission to a servlet which will call your database state update code. I'm no Javascript expert but there is a suggested solution
on Java Junction. Just as full disclosure, I haven't used this.
The other option, is the one that Jan suggested earlier.
What some applications (say: SAP) do, is,
when you log on, and it seems that you still have a log on active,
it allows you to choose:
a - stop this new log on : the new session closes.
b - stop the old log on, and allow the new session:
From that moment on, if the old session would still exist, the system would reject all activity in the old session, and only requests of the new session would be handled
Personally, I think this is preferable but maybe that's just my fear of Javascript talking :-)