Session Management
Overview
Let’s learn about session management in this tutorial. HTTP is a stateless protocol. When a web browser sends a request to the web server, the web server provides a response to the web browser and closes the connection.
The server doesn’t store any information about the client. Sometimes we want the server-side program to identify what values are given to the clients in the previous response.
Session Management
Session management makes the server-side program identify what values are given to the clients in the previous response. Session management can be achieved in multiple different ways:
- Hidden Variables
- Cookies
- Sessions
Hidden Variables
A hidden variable is an input field in a form that contains a value that will not be displayed on the HTML document. Whenever the user clicks on the submit button the value of the hidden variable will also be submitted along with the other input fields to the web server.
Cookies
A cookie is a piece of information created by the web server and will be passed to the web browser. The browser receives the cookie and stores the cookie in a separate memory location on the client’s computer.
A cookie contains the name and value. In Java programming language, cookies are managed using the Cookie class. Servlets also support cookies using the javax.servlet.http.Cookies class.
We can add cookies with the addCookies(cookie) method of the HttpServletResponse object. We can get the list of cookies added with getCookies() method HttpServletRequest object.
Sessions
A session can be defined as a series of related interactions between a single web client and the web server which take place over a period of time. A session id is used to track the session. The session id is passed between the client and server to identify the session. By sending the session id back and forth, the client and web server are able to maintain an ongoing session. The session-tracking mechanism allows the collection of objects to be associated with the session.