Why Session Tracking?
- There is a need for many applications to maintain data across multiple client requests.
- Example: On-line Shopping Cart
- User travels around different web-pages, looking at products
- Occasionally saves an item in their shopping cart.
General Solutions:
1) Cookies2) URL-rewriting |
Servlet Solutions
|
|||||||||||||||
Servlet Session Tracking API
When the client makes a HTTP request of the Servlet (e.g. will invoke the doGet or doPost methods)1) Create or Retrieve the session associated with this client.Store information as needed in this session object.Note: the first time the Servlet recieves a request from client A it will not have a session associated with it, because the Servlet did not yet explicitly create it yet. The nth time the Servlet recieves a request from client A, if it had on a previous request from this same client, created a session, it will then retrieve this same session rather than creating a new one.2) Add/Remove/Lookup attribute values stored with the session as indicated by the task at hand (what the servlet is being requested to do).
|
|||||||||||||||
Servlet Code using Session Tracking API
Here is a piece of the code from the example in
Section 9.4 of your Core Servlet book:
|