Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
HydraExpress Servlet Development Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

8.2 Saving Session State

The servlet container maintains state using sessions. Each session holds data associated with a particular client. The servlet container issues a unique identifier to each new session. When a request arrives, the container matches the identifier on the incoming request to a session object and attaches the session object to the request.

A session stores state as a collection of attributes, indexed by name. Each attribute is an instance of rwsf::Attribute, a helper class that holds objects of nearly any type (see Section 6.3).

To save state in a session, the servlet creates an attribute object and stores data in the object. The servlet then retrieves the session from the request object and adds the attribute to the session.

For example, the code below saves a shopping cart object in a session:

//1Constructs an empty attribute object.
//2Copies cart into cartAttr.
//3Retrieves a session object from the request. If the client already has a session active, getSession() returns that session. Otherwise, getSession() creates a new session for the client, and returns the new session.
//4Stores the attribute in the session under the name "ShoppingCart".

Notice that if a client sends multiple or overlapping requests, more than one thread may have access to the same session at the same time. The servlet is responsible for managing concurrency issues on attributes stored within sessions.

See Section 7.5, "Recovering Session State," for information about recovering state from a session and information on managing new sessions.

Previous fileTop of DocumentContentsIndex pageNext file

©2004-2007 Copyright Quovadx, Inc. All Rights Reserved.
Quovadx and Rogue Wave are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.