The Session Object in JSP

By: Sathya Narayana Viewed: 153497 times  Printer Friendly Format    


The session object is an instance of a class that implements the javax.servlet.http.HttpSession interface. You can use it to store session state for a user, and it makes the following methods available.

getAttribute() returns the Object bound to the specified name in this session or null if it doesn't exist:

public Object getAttribute(String name)

getAttributeNames() returns an Enumeration of String objects containing the names of all the objects bound to this session:

public java.util.Enumeration getAttributeNames()

getCreationTime() returns the time when the session was created in milli-seconds since midnight January 1, 2003, GMT:

public long getCreationTime()

getId() returns a String object containing a unique identifier for this session:

public String getId()

getLastAccessedTime() returns the last time a client request associated with the session was sent. The return value is the number of milliseconds since mid-night January 1, 1970, GMT:

public long getLastAccessedTime()

getMaxInactiveInterval() returns the number of seconds the server will wait between client requests before the session is invalidated. A negative return value indicates that the session will never time out:

public int getMaxInactiveInterval()


getServletContext() returns the ServletContext of this session:

public ServletContext getServletContext()

invalidate() invalidates the session and unbinds any objects bound to it:

public void invalidate()

isNew() returns true if the server has created a session that hasn't yet been accessed by a client:

public boolean isNew()

removeAttribute() removes the Object bound to the specified name from this session:

public void removeAttribute(String name)

setAttribute() binds an Object to the specified attribute name in this session. If the attribute name already exists, the Object passed to this method will replace the previous Object:

public void setAttribute(String name,
Object value)

setMaxInactiveInterval() specifies the number of seconds the server will wait between client requests before the session is invalidated. If a negative value is passed to this method, the session will never time out:

public void setMaxInactiveInterval(int interval)

These methods are deprecated and shouldn't be used in new code—they exist for compatibility with existing code:

public HttpSessionContext getSessionContext()
public Object getValue(String name)
public String[] getValueNames()
public void putValue(String name,
Object value)
public void removeValue(String name)



Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial