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
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.