Java Servlet Programming, 2nd Edition > B. HTTP Servlet API Quick Reference > HttpSession |
SynopsisInterface Name: javax.servlet.http.HttpSession Superinterface: None Immediate Subinterfaces: None Implemented By: None Availability: Servlet API 2.0 and later DescriptionThe HttpSession interface provides a mechanism for holding temporary information about visitors to a web site. For a detailed introduction to session tracking, see Chapter 7. The HttpSession interface itself allows servlets to view and manipulate session-specific information, such as creation time and the unique session identifier. It also includes methods to bind objects to the session for later retrieval, allowing "shopping cart" and other applications to hold onto data between client requests. A servlet obtains an HttpSession object from the getSession( ) method of HttpServletRequest. Session behavior, such as the amount of idle time before a session is destroyed, can be configured programmatically and via the web application deployment descriptor. For sessions inside a nondistributed web application, any object may be bound to the session. Objects that implement java.io.Serializable may be written to disk to save memory and to persist between server restarts. For sessions inside a distributed web application, objects placed into the session must implement java.io.Serializable. The server may throw an IllegalArgumentException when this requirement is not met. Servers use session affinity to efficiently manage sessions in a distributed environment where there are multiple backend servers. This means that all requests that are part of a single session from a particular user are handled by only one JVM at a time. This eliminates the need to constantly replicate session information across all the backend servers. Responsibility for the session can be moved to another server between user requests, and to enable the moving of a session all objects placed into a session must be Serializable. Interface Declarationpublic interface HttpSession { // Methods // Most methods may throw IllegalStateException public abstract Object getAttribute(String name); // New in 2.2 public abstract Enumeration getAttributeNames(); // New in 2.2 public abstract long getCreationTime(); public abstract String getId(); public abstract long getLastAccessedTime(); public abstract int getMaxInactiveInterval(); // New in 2.1 public abstract HttpSessionContext getSessionContext(); // Deprecated public abstract Object getValue(String name); // Deprecated public abstract String[] getValueNames(); // Deprecated public abstract void invalidate(); public abstract boolean isNew(); public abstract void putValue(String name, Object value); // Deprecated public abstract void removeAttribute(String name); // New in 2.2 public abstract void removeValue(String name); // Deprecated public abstract void setAttribute(String name, Object value);// New in 2.2 public abstract void setMaxInactiveInterval(int secs); // New in 2.1 } MethodsgetAttribute()public abstract Object getAttribute(String name) throws IllegalStateException
getAttributeNames()public abstract Enumeration getAttributeNames() throws IllegalStateException
getCreationTime()public abstract long getCreationTime() throws IllegalStateException
getId()public abstract String getId() throws IllegalStateException
getLastAccessedTime()public abstract long getLastAccessedTime() throws IllegalStateException
getMaxInactiveInterval()public abstract int getMaxInactiveInterval() throws IllegalStateException
getSessionContext()public abstract HttpSessionContext getSessionContext()
getValue()public abstract Object getValue(String name) throws IllegalStateException
getValueNames()public abstract String[] getValueNames() throws IllegalStateException
invalidate()public abstract void invalidate() throws IllegalStateException
isNew()public abstract boolean isNew() throws IllegalStateException
putValue()public abstract void putValue(String name, Object value) throws IllegalStateException
removeAttribute()public abstract void removeAttribute(String name) throws IllegalStateException
removeValue()public abstract void removeValue(String name) throws IllegalStateException
setAttribute()public abstract void setAttribute(String name, Object value) throws IllegalStateException
setMaxInactiveInterval()public abstract void setMaxInactiveInterval(int secs)
|