Advertisement
Constructor | Description |
---|---|
void Cookie(String name, String value) | Constructs a Cookie with a name and a value. |
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. |
<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>
<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
<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>
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement