Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using Sessions

The HTTP protocol is by its very nature stateless—that is, when you load a page a number of times in succession, the server starts anew each time. If you want to track, say, the number of times the user has accessed the page, you need to specifically store that data somewhere, as in a cookie.

This is very different from writing a standard program, where you're interacting with the user while the program is running, so you can store data in variables and count on it not being reset to its default values the next time you look at that data. Using sessions, you actually can do the same thing in JSP—you can interact with the user as though you have a session going.

What does that mean? During the time a session is active, you can store data in the session, and it'll be preserved on the server in between times the user accesses your page. You can set your variables in one page access, and they'll hold the same data the next time you see the page, as long as the session hasn't timed out (see "Setting Session Timeouts" later today). The default time Tomcat allows between page accesses in a session is 30 minutes.

You can access the current session using the built-in JSP session object, which is based on the javax.servlet.http.HttpSession interface. The data you store in a session is stored as session attributes. You can use the session object's setAttribute method to store data, and the getAttribute method to recover that data, as long as the session is active. Each session is given its own ID value, and when a session first begins, the session object's isNew method will return a value of true.


You should realize that it might not be possible for Tomcat to set up a session with the browser. Tomcat usually uses cookies to store session information, but the browser may have cookies turned off, for example (in that case, Tomcat tries to encode the needed information in URLs, but sometimes that is not successful). If Tomcat is not able to create a session with the browser, the isNew method will keep returning true every time the page is accessed, and the session ID will be different for each page access. You can also use the isRequestedSessionIdFromURL and isRequestedSessionIdFromCookie methods to see how Tomcat is storing session information—see Table 4.2.

Those are just a few of the methods of the javax.servlet.http.HttpSession interface—you can see them in Table 7.4.

Table 7.4 javax.servlet.http.HttpSession Methods


Does This

void addCookie(Cookie cookie)

Adds the specified cookie to the response object.

java.lang.Object getAttribute (java.lang.String name)

Returns the object of the given name in this session.

java.util.Enumeration getAttributeNames()

Returns a Java Enumeration of String objects containing the names of all the objects in this session.

long getCreationTime()

Returns the time when this session was created (measured in milliseconds since midnight January 1, 1970 GMT).

java.lang.String getId()

Returns a string containing the identifier for this session.

long getLastAccessedTime()

Returns the last time the client sent a request in with this session, as the number of milliseconds since midnight January 1, 1970 GMT.

int getMaxInactiveInterval()

Returns the maximum time, in seconds, which the server will keep this session open between client accesses.

ServletContext getServletContext()

Returns the ServletContext to which this session belongs.

HttpSessionContext getSessionContext()

As of servlet specification version 2.1, this method is deprecated.

java.lang.Object getValue(java.lang.String name)

Deprecated. As of servlet specification version 2.2, this method is replaced by getAttribute(java.lang.String).

java.lang.String[] getValueNames()

Deprecated. As of servlet specification version 2.2, this method is replaced by getAttributeNames().

void invalidate()

Invalidates this session.

boolean isNew()

Returns true if the client does not yet know about the session.

void putValue(java.lang.String name, java.lang.Object value)

Deprecated. As of servlet specification version 2.2, this method is replaced by setAttribute(java.lang.String, java.lang.Object).

void removeAttribute(java.lang.String name)

Removes the object with the specified name from this session.

void removeValue(java.lang.String name)

As of servlet specification version 2.2, this method is replaced by removeAttribute(java.lang.String).

void setAttribute(java.lang.String name, java.lang.Object value)

Connects an object to this session, using the given name.

void setMaxInactiveInterval(int interval)

Specifies the time, in seconds, between client requests before the server will invalidate this session.

  • + Share This
  • 🔖 Save To Your Account