State Management:- When we work on the web generally it uses HTTP(Hyper Text Transfer Protocol) or HTTPS(Hyper Text Transfer protocol secured) to deliver text, sound, images, graphics etc on the web. As the name shows it is a Protocol for the web.
HTTP protocol is a stateless protocol that does not maintain the state of client/user on the web server. As the client submit a new request from the web browser a new Http packet is created and as the response received from the client(browser) after processing the response content, Http packet is destroyed.the server does not maintain the state of the client on the server for further interaction.
In today's web applications it is required to maintain the state of every client come to the server so that once a client makes the request to server the server can manage information about a client by means of some token etc.
There are following four ways to in web for state management.
1.Hidden Form Field
2.URL Rewriting(Query String)
Hidden Form Field:- This scheme of state management prepares the Hidden Text Field and populate the data submitted in the current request into the hidden text field and hide that field as the response to the browser for the resubmission of data in further requests to the server.
<input type="hidden" name="secret" value="124535"/>
<input type="submit" value="submit"/>
on the server, you can get this value from
String secretValue = req.getParameter("secret");
URL Rewriting:- in this scheme, the required data is sent within URL request as a query string with ? value.the web server prepares and returns the hyperlink with the query string so that with next request submitted with the hyperlink the same data resubmit on the server
<a href='shopping?user="+value+"'>goto shop</a>
Cookie:- Cookie is a small file having size approx 4KB which stores data in text format in the form of key and value pair. It is up to the client whether it wants to store the cookie or not, it can change cookie storage setting from the browser.
First Time when a client makes the request to a server, the server send a cookie contains information about client along with the response. In the client make the further request to the same server for each request the cookie is also sent along with the request by the browser and server identifies the client by that cookie and maintain the state of a client for requests.cookie can be persistent and not persistent.the persistent cookie is saved even after closing browser.you can set the time for the cookie as well.
Cookie cookie = new Cookie("user","kuldeep");