Advertisement



< Prev
Next >



JSP Cookie





Cookie is one of the most common ways to track a user on the internet. Cookie is a text file using which you can store the information in a user's browser and access it when you need it to communicate with the user or to manage the user's session. You can even specify the duration for which this cookie file is going exist before it gets deleted by the browser.

JSP allows us to perform a user's session management using its Cookie class.




Constructor of Cookie class


Constructor Description
void Cookie(String name, String value) Constructs a Cookie with a name and a value.





Some useful methods of Cookie class.


The methods in the table below are part of javax.servlet.http.Cookie class, hence these methods are available to the object of Cookie class.

Methods Description
void setDomain(String name) Sets the domain name which will be associated with the Cookie.
String getDomain() Returns the domain name associated with the Cookie.
void setMaxAge(int expiry) Sets the maximum time(in seconds) till the Cookie lasts.
int getMaxAge() Returns the maximum time(in seconds) till the Cookie lasts.
void setComment(String message) Sets a message/comment describing the Cookie.
String getComment() Returns the message describing the Cookie.
void setValue(String value) Sets the value of the Cookie that is already created.
String getValue() Returns the value of the Cookie.





Creating, setting and reading a Cookie using JSP


In this example, we are going to create, set and read a Cookie using JSP's Cookie class and its methods.


CreateCookie.jsp
<html>
<head>	
<title>Ceating a JSP Cookie</title>
</head>

<body>

<h2>Creating and setting a Cookie using response object</h2>
<br/>
<br/>

<%

// Creating a Cookie
Cookie cookie = new Cookie("Name", "DecodeJava");
cookie.setMaxAge(60*60);

// Setting the Cookie in the browser
response.addCookie(cookie);

%>

<br/>

Do you want to read the Cookie? If yes, please visit <a href ="ReadCookie.jsp" /> Read the Cookie</a>
<br/>

Do you want to delete the Cookie? if yes, please visit the  <a href ="DeleteCookie.jsp" /> Delete the Cookie</a>

</body>
</html>





ReadCookie.jsp
<html>
<head>	
<title>Ceating a JSP Cookie</title>
</head>

<body>

<h2>Reading the cookies set by the request object.</h2>

<% 
Cookie[] array= request.getCookies();
for(int i=0; i<array.length; i++)
{
if(array[i].getName().equals("Name"))
{
out.println("<br/>");
out.println("Name of the cookie : " + array[i].getName() + "<br/>");
out.println("Value in cookie : " + array[i].getValue());
} 
}
%>


</body>
</html>






Advertisement




Deleting a Cookie using JSP


We can delete an existing cookie in two steps -


DeleteCookie.jsp
<html>
<head>	
<title>Ceating a JSP Cookie</title>
</head>

<body>

<h2>Deleting a Cookie using response object</h2>
<br/>
<br/>

<%
Cookie[] array= request.getCookies(); 
for(int i=0; i<array.length; i++)
{
if(array[i].getName().equals("Name"))
{
out.println("<br/>");
out.println("Name of the cookie : " + array[i].getName() + "<br/>");
out.println("Value in cookie : " + array[i].getValue());

//Deleting the cookie
array[i].setMaxAge(0);
response.addCookie(array[i]); //After this, although cookie will be not be visible from browser's memory
			      //but Cookie data will still be visible until you refresh this page.

} 
}
%>
<br/>

</body>
</html>









Please share this article -




< Prev
Next >
< JSP Built-in Methods
Handling Exceptions in JSP >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement