How Will You Use a Cookie in JSP ?

We can use a Cookie in JSP by performing following steps..

First we create a Cookie object. We set the name and value of the cookie to be created.

We set the expiry time of the Cookie by setting the maximum age. We can use setMaxAge() method for this.

Finally, we can send the cookie in a HTTP Response by sending it in HTTP header. In this way cookie goes to client browser and gets stored there till the maximum age is not achieved.

Once a Cookie is set in the client browser, we can call getCookies() method to get the list of all the cookies set in Client. We iterate through the list of all the cookies and get the value of the cookie that was set in earlier request.

In this way we can use Cookie to set some information at client side and retrieve its value.



You May Interest

What is the Difference Between Abstract Class and Interface in Ja ...

Enumeration and Iterator, Which One Has Better Performance in Jav ...

What are the Different States of a Thread in Java ?

How Can We Prevent a Deadlock in Java ?

What are the Thread-safe Classes in Java Collections Framework ?