|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
The HttpSession interface is implemented by services to provide an association between an HTTP client and HTTP server. This association, or session, persists over multiple connections and/or requests during a given time period. Sessions are used to maintain state and user identity across multiple page requests.
A session can be maintained either by using cookies or by URL rewriting.
HttpSession defines methods which store these types of data:
The following code snippet illustrates getting and setting the the session data value.
//Get the session object - "request" represents the HTTP servlet request HttpSession session = request.getSession(true);
//Get the session data value - an Integer object is read from //the session, incremented, then written back to the session. //sessiontest.counter identifies values in the session Integer ival = (Integer) session.getValue("sessiontest.counter"); if (ival==null) ival = new Integer(1); else ival = new Integer(ival.intValue() + 1); session.putValue("sessiontest.counter", ival);
When an application layer stores or removes data from the session, the session layer checks whether the object implements HttpSessionBindingListener. If it does, then the object is notified that it has been bound or unbound from the session.
An implementation of HttpSession represents the server's view of the session. The server considers a session to be new until it has been joined by the client. Until the client joins the session, the isNew method returns true. A value of true can indicate one of these three cases:
It is the responsibility of developers to design their applications to account for situations where a client has not joined a session. For example, in the following code snippet isNew is called to determine whether a session is new. If it is, the server will require the client to start a session by directing the client to a welcome page welcomeURL where a user might be required to enter some information and send it to the server before gaining access to subsequent pages.
//Get the session object - "request" represents the HTTP servlet request HttpSession session = request.getSession(true);
//insist that the client starts a session //before access to data is allowed //"response" represents the HTTP servlet response if (session.isNew()) { response.sendRedirect (welcomeURL); }
HttpSessionBindingListener
,
HttpSessionContext
Method Summary | |
long |
getCreationTime()
Returns the time at which this session representation was created, in milliseconds since midnight, January 1, 1970 UTC. |
java.lang.String |
getId()
Returns the identifier assigned to this session. |
long |
getLastAccessedTime()
Returns the last time the client sent a request carrying the identifier assigned to the session. |
int |
getMaxInactiveInterval()
|
HttpSessionContext |
getSessionContext()
Deprecated. |
java.lang.Object |
getValue(java.lang.String name)
Returns the object bound to the given name in the session's application layer data. |
java.lang.String[] |
getValueNames()
Returns an array of the names of all the application layer data objects bound into the session. |
void |
invalidate()
Causes this representation of the session to be invalidated and removed from its context. |
boolean |
isNew()
A session is considered to be "new" if it has been created by the server, but the client has not yet acknowledged joining the session. |
void |
putValue(java.lang.String name,
java.lang.Object value)
Binds the specified object into the session's application layer data with the given name. |
void |
removeValue(java.lang.String name)
Removes the object bound to the given name in the session's application layer data. |
void |
setMaxInactiveInterval(int interval)
Sets the maximum interval between requests that this session will be kept by the host server. |
Method Detail |
public long getCreationTime()
public java.lang.String getId()
public long getLastAccessedTime()
This information is particularly useful in session management policies. For example,
public int getMaxInactiveInterval()
public HttpSessionContext getSessionContext()
public java.lang.Object getValue(java.lang.String name)
name
- the name of the binding to findpublic java.lang.String[] getValueNames()
public void invalidate()
public boolean isNew()
public void putValue(java.lang.String name, java.lang.Object value)
name
- the name to which the data object will be bound. This
parameter cannot be null.value
- the data object to be bound. This parameter cannot be null.public void removeValue(java.lang.String name)
name
- the name of the object to removepublic void setMaxInactiveInterval(int interval)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |