Sessions
Sessions
What is a Session?
A session is a state associated with particular user
that is maintained at the server side.
Sessions persist between the HTTP requests Sessions
enable creating applications that depend on
individual user data. For example:
• Login / logout functionality
• Wizard pages
• Shopping carts
• Personalization services
• Maintaining state about the user’s preferences
Sessions in Servlets
• Servlets include a built-in Session s API.
• Sessions are maintained automatically.
• The Web container associates an unique
HttpSession object to each different client.
• Different clients have different session objects
at the server
• Requests from the same client have the same
session object
• Sessions can store various data
The Sessions API
The sessions API allows
• To get the HttpSession object from the
HTTPServletRequest object.
• Extract data from the user’s session object.
• Append data to the user’s session object
• Extract meta-information about the session
object, e.g. when was the session created
Behind The Scenes
• When you call getSession() each user is automatically
assigned a unique Session ID
How does this Session ID get to the user?
• Option 1: If the browser supports cookies, the servlet
will automatically create a session cookie, and store
the session ID within the cookie
• Option 2: If the browser does not support cookies, the
servlet will try to extract the session ID from the URL
• Option 3:In case of Hidden Form Field a hidden
(invisible) textfield is used for maintaining the state of
an user.
HttpSession Interface
• Container creates a session id for each user.
The container uses this id to identify the
particular user.
• An object of HttpSession can be used to
perform two tasks:
bind objects
view and manipulate information about a
session, such as the session identifier, creation
time, and last accessed time.
How to get the HttpSession object ?
• public HttpSession getSession():Returns the
current session associated with this request, or if
the request does not have a session, creates one.