Each time the web browser sends the request back, the session_id value can be used to keep the track of different web browsers.This can be an effective way of keeping track of the session but clicking on a regular () hypertext link does not result in a form submission, so hidden form fields also cannot support general session tracking.Fortunately for us servlet developers, it's not always necessary for a servlet to manage its own sessions using the techniques we have just discussed.The Servlet API provides several methods and classes specifically designed to handle session tracking on behalf of servlets.What can be an alternative solution to invalidate a user session on browser closure ?Session (in)validation is usually a server concept: a session is "valid" as long as the server considers it to be valid, i.e.
You cannot reliably handle it client-side only, if only because the client can disappear abruptly without any trace (user's laptop battery is empty, user is roaming in a car/train and goes out of range of a station, user's machine or browser crashes, user is evil and kills his browser just to annoy you... What if we don't keep the session at the server side?Let us create a Servlet Listener that just counts the number of running http sessions and prints the details whenever a session gets created or destroy.Starts eclipse and create a new dynamic web project with name Session Listener. I have selected Apache Tomcat v6.0, you can select any Tomcat version that you have installed. method will be called by the servlet container whenever a new session is created for this application. Http Session Event class is passed as an argument to the session Created method.Whenever a new session is created, this count gets incremented.The method will be called by the servlet container whenever an existing session is invalidated.