Saturday, April 25, 2009

Clear session on Browser Close

In web application, it is common that user sign in, do some work and close browser(By clicking X) with doing proper Sign Out. In such case, user session is still active and will be cleared only after specified session out period. If the specified timeout is small than it should not be problem. But if specified timeout is long in that case it is important to clear user session on browser close becuase clearing session on server will remove unused objects from server and hence reduce memory leak.

Here is possible solutions to handle it.

1) One approach is to use frames in your application. After user Sign In, Load application in Frameset as below.


<FRAMESET rows="0, *" onbeforeunload="javascript:callServer()">
<FRAME src="sessionFrame"> // Blank frame unloaded when browser close.
<FRAME src="mainFrame"> // Your Application pages
</FRAMESET>



When user closes browser(By clicking X), it will unload frameset and call callServer() javascript function. In callServer() function make server call(using XMLHttpRequest or submit Html Form) to clear session.

0 Java Junction comments: