Session Tracking
Why?
When clients at an on line store add an item to their
shopping cart, how does the server know what's already
in the cart?
When clients decide to proceed to checkout, how can the
server determine which previously created shopping cart
is theirs?
How?
Cookies
URL rewriting
Hidden form fields
Higher level API needed
87
Servlets
www.corewebprogramming.com
The Session Tracking API
Session objects live on the server
Automatically associated with client via
cookies or URL rewriting
Use
request.getSession(true)
to get either existing or new
session
Behind the scenes, the system looks at cookie or
URL extra info and sees if it matches the key to some
previously stored session object. If so, it returns that
object. If not, it creates a new one, assigns a cookie
or URL info as its key, and returns that new session
object.
Hashtable like mechanism lets you store
arbitrary objects inside session
setAttribute
stores values
getAttribute
retrieves values
88
Servlets
www.corewebprogramming.com
44