Session Tracking with HttpSession

Session tracking means that the container is able to associate a request with a client, and HttpSession object represents that association. The web container maintains this object for the duration of the client session, or a configurable timeout period. Since many clients interact with container, the container maintains individual HttpSession objects for each client. By using methods of HttpSession we can associate the client state also with the session object. The Servlet API for session tracking is as follows.

•     Javax.Servlet.http.HttpServletRequest for the creation and retrieval of HttpSession objects.

•     Javax.Servlet.http.HttpSession interface for representing sessions. This interface provides methods for state maintenance also.

HttpServletRequest interface provides the following method to create or access the session object.

HttpSession getSession(): The above method returns the reference to a container created object that implements HttpSession. If there is no session associated with the current request, the above method creates one. If already a session is associated with the client, it gives the reference to the existing session object. List of important methods in HttpSession Methods for session lifetime:-

•     public long getCreation Time():- Returns the time that the session was created, in milliseconds since Jan 1, 1970 00.00 hrs.

•     public String getld():- Returns a String containing a unique identifier assigned to this session,

•     public long getLastAccessedTime():- Returns the time that the session was last accessed by the client, in milliseconds since Jan 1, 1970 OO.OOhrs. This method can be used to determine the period of inactivity between two consecutive requests from a client.

•     public int getMaxInactiveInterval():-Returns the length of time in seconds that the session will remain active between requests before expiring.

•     public void setMaxInactiveInterval(intseconds):-This methods sets the length of time in seconds that the session will remain active between requests before expiring. The web container makes sure that the session is automatically invalidated after expiry of this interval.

•     public boolean isNew():- Returns true if the client does not yet know about the session or if the client chooses not to join the session. A client is considered to join a session when the client returns session tracking information previously sent by the server.

•     public void invalidate():- Invalidates this session then unbinds any objects bound to it.

Methods for associating state with session:-

•     Public void setAttribute(String name.Object value):- Stores a value in the session object.

•     Public Object getAttribute(String name):- Returns the object bound with the specified name in this session, or null if no object is bound under the name.

•     Public void removeAttribute(String name):- Removes the object from this session.

•     Public Enumeration getAttributeNames():- Returns an Enumeration of String objects bound to the session.