|
Using Cookies
Part:
1
2
3
4
5
(Continued from previous part...)
The following JSP page shows you how to set a persistent cookie and how to use other
properties.
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!--
- CookieProperties.jsp
- Copyright (c) 2003 by Dr. Herong Yang, http://www.herongyang.com/
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<p>
<jsp:directive.page import="javax.servlet.http.Cookie"/>
<jsp:scriptlet><![CDATA[
// Setting a cookie with default properties
out.println("<b>Cookie with default properties:</b><br/>");
Cookie c = new Cookie("Date","30-Mar-2003");
response.addCookie(c);
out.println("Name: "+c.getName()+"<br/>");
out.println("Value: "+c.getValue()+"<br/>");
out.println("Domain: "+c.getDomain()+"<br/>");
out.println("Path: "+c.getPath()+"<br/>");
out.println("MaxAge: "+c.getMaxAge()+"<br/>");
out.println("Version: "+c.getVersion()+"<br/>");
// Setting a cookie with specified properties
out.println("<b>Cookie with specified properties:</b><br/>");
c = new Cookie("User","Herong Yang");
c.setMaxAge(3*24*60*60);
response.addCookie(c);
out.println("Name: "+c.getName()+"<br/>");
out.println("Value: "+c.getValue()+"<br/>");
out.println("Domain: "+c.getDomain()+"<br/>");
out.println("Path: "+c.getPath()+"<br/>");
out.println("MaxAge: "+c.getMaxAge()+"<br/>");
out.println("Version: "+c.getVersion()+"<br/>");
// Checking properties of the received cookies
out.println("<b>Properties of the received cookies:</b><br/>");
Cookie[] cookies = request.getCookies();
int n = 0;
if (cookies!=null) {
n = cookies.length;
for (int i=0; i<cookies.length; i++) {
out.println("Name: "+cookies[i].getName()+"<br/>");
out.println("Value: "+cookies[i].getValue()+"<br/>");
out.println("Domain: "+cookies[i].getDomain()+"<br/>");
out.println("Path: "+cookies[i].getPath()+"<br/>");
out.println("MaxAge: "+cookies[i].getMaxAge()+"<br/>");
out.println("Version: "+cookies[i].getVersion()+"<br/>");
}
}
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>
So I opened this page with IE, and got:
Cookie with default properties:
Name: Date
Value: 30-Mar-2003
Domain: null
Path: null
MaxAge: -1
Version: 0
Cookie with specified properties:
Name: User
Value: Herong Yang
Domain: null
Path: null
MaxAge: 259200
Version: 0
Properties of the received cookies:
Then I clicked at IE "Tools" menu, selected "Internet Options...".
and clicked the "Settings..." button in the "Temporary Internet files" section of
the "General" tab. I saw where is my "Temporary Internet files folder".
So I went to that folder, and saw a cookie file named something like
"Cookie:user@localhost/jsp/". I double clicked on that file, and was able to
open it in notepad:
User
Herong Yang
localhost/jsp/
1024
2353942784
29567146
224352272
29566543
*
(Continued on next part...)
Part:
1
2
3
4
5
|