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 are the Similarities Between HashSet and HashMap in Java ?

What is XMLBeanFactory in Spring Framework ?

Why Do We Use Nested Classes in Java ?

Why ListIterator Has add Method But Iterator Does Not Have in Jav ...

What are the Examples of Observer Design Pattern in JDK ?