Web container yendor is required to support sessions. Sessions are mechanism for storing the client-specific data across multiple HTTP requests. This is called session scope
Use an HttpSession
We can use an HttpSession object to hold the conversational state across the multiple request . In other words for the entire session for the client.
How the container will know who the client is ?
HTTP uses the stateless connections . The client browser makes the connection to the server, sends the request and get the response and closes the request . In other words connection exists for only single request/response.
Because the connection does not persist , the container does recognize that the client making the second request is the same client from the previos request. Container concerns that each request is the new request.
The idea is simple on the client first request, the container generates the unique session ID and give it back to the client with each response The client sends back the session ID with each request. The container sees the Id , finds the matching session, and associates the session with the client.
How the client and container exchange session Information?
The container has to get the session ID from the client as the part of the response and the client has to send back the session ID as the part of the request. The simplest and most common way to exchange through cookies. The best part is container does all these things virtually. You do have to tell the container that you want to use session but the container takes care of generating the session ID, creating the cookie object stuffing session ID into cookies and settind the cookie as a part of response . And subsequently request the container to get the session the session ID from the cookie in the request
Sending the session cookie in response
To know wether the session is already existed there is method called isNew()