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 Using rwsf::Attribute).
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 << (std::string)"cart"; // 2
 
rwsf::HttpSession session = request.getSession(true); // 3
 
session.setAttribute("ShoppingCart", cartAttr); // 4
//1 Constructs an empty attribute object.
//2 Stores the string value “cart” in the created attribute instance.
//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 Recovering Session State for information about recovering state from a session and information on managing new sessions.