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 Differences Between a Vector and an ArrayList in Jav ...

What are the Differences Between a Checked and Unchecked Exceptio ...

How Can We Prevent Busy Waiting in Java ?

What is the Difference Between a Cookie and a Session Object in J ...

Why Do You Use Upcasting or Downcasting in Java ?