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 Fail-fast and Fail-safe Iterator i ...

What is the Use of Run Time Polymorphism in Java ?

What is the Difference Between an ArrayList and a LinkedList Data ...

How Can We Find the Memory Usage of JVM From Java Code ?

What is the Difference Between an Iterator and ListIterator in Ja ...