5. Designing and Developing Servlets Using Session Management
5.1. Identify the interface and method for each of the following:
5.1.1. retrieve a session object across multiple requests
to the same or different servlets within the same webapp
5.1.1.1. public HttpSession HttpServletRequest.getSession([boolean
create])
5.1.1.2. if no argument provided, then server
will automatically create a new session object if none exists
for the user in the web app context
5.1.1.3. to make
sure the session is properly maintained, getSession must
be called at least once before committing the response
5.1.1.4. sessions are scoped at the web application level;
so a servlet running inside one context cannot access session
information saved by another context.
5.1.1.5. behind
the scenes, the client's session id is usually saved on
the client in a cookie called JSESSIONID. For client that
don't support cookies, the session ID can be sent as part
of a rewritten URL, encoded using a jsessionid path parameter.
5.1.1.6. note that a requested session id may not match
the id of the session returned by the getSession() method,
such as when the id is invalid. one can call req.isRequestedSessionIDValid()
to test if the requested session id (that which was defined
in the rewritten url or the persistent cookie) is valid.
5.1.2. store objects into a session object
5.1.2.1.
public void HttpSession.setAttribute(String name, Object
value) throws IllegalStateException
5.1.2.2. binds the
specified object under the specified name. Any existing
binding with the same name is replaced.
5.1.2.3. IllegalStateException
thrown if session being accessed is invalid
5.1.3. retrieve
objects from a session object
5.1.3.1. public Object
HttpSession.getAttribute(String name) throws IllegalStateException
-- returns the object bound under the specified name or
null if there is no binding
5.1.3.2. public Enumeration
HttpSession.getAttributeNames() throws IllegalStateException
-- returns all bound attribute names as an enumeration of
Strings (empty enum if no bindings)
5.1.3.3. public void
HttpSession.removeAttribute(String name) throws IllegalStateException
-- removes binding or does nothing if binding does not exist