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:
// cart is a ShoppingCart object rwsf::Attribute cartAttr; // 1 cartAttr << cart; // 2 rwsf::HttpSession session = request.getSession(); // 3 session.setAttribute("ShoppingCart", cartAttr); // 4
|//1||Constructs an empty attribute object.|
|//2||Copies cart into cartAttr.|
|//3||Retrieves 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.|
|//4||Stores 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.
©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.