Persistent Cookie Test Example

This section provides a tutorial example on how to set a persistent cookie and set other properties of a cookie in JSP page.

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="2.3">
<!--
 - CookieProperties.jspx
 - Copyright (c) 2006 HerongYang.com. All Rights Reserved.
-->
<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","01-Jan-2099");
   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"); // space is not allowed
   c = new Cookie("User","HerongYang");
   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>

Visit this page with IE, and you should see:

Cookie with default properties:
Name: Date
Value: 01-Jan-2099
Domain: null
Path: null
MaxAge: -1
Version: 0
Cookie with specified properties:
Name: User
Value: HerongYang
Domain: null
Path: null
MaxAge: 259200
Version: 0
Properties of the received cookies:

If you are using IE, click at IE "Settings > Tools" menu, selected "Internet Options...", and clicked the "Settings..." button in the "Browsing history" section of the "General" tab. You will see the "Temporary Internet Files and History Settings" dialog box.

Click "View files" button, you will see the cookie file called "cookie:herong@localhost/". Double click on that file, and it will open in Notepad:

User
HerongYang
localhost/
1024
2353942784
29567146
224352272
29566543
*

This proves that cookie "User=HerongYang" is persisted in a file on the hard disk.

Then you click the IE refresh button, you should get the following in the IE window:

Cookie with default properties:
Name: Date
Value: 01-Jan-2099
Domain: null
Path: null
MaxAge: -1
Version: 0
Cookie with specified properties:
Name: User
Value: HerongYang
Domain: null
Path: null
MaxAge: 259200
Version: 0

Properties of the received cookies:
Name: Date
Value: 01-Jan-2099
Domain: null
Path: null
MaxAge: -1
Version: 0
Name: User
Value: HerongYang
Domain: null
Path: null
MaxAge: -1
Version: 0
Name: JSESSIONID
Value: 37CB87D855A94F84355FE86626D2BAF7
Domain: null
Path: null
MaxAge: -1
Version: 0

It is interesting to know that:

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

Managing Cookies in JSP Pages

 What Is a Cookie

 Sending and Receiving Cookies in JSP Pages

 Persistent Cookies Stored on Hard Disk

Persistent Cookie Test Example

 Space Character not Allowed in Cookie Value

 Dumping HTTP Response with Cookies

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Using Tomcat on CentOS Systems

 Using Tomcat on macOS Systems

 Connecting to SQL Server from Servlet

 Developing Web Applications with Servlet

 Archived Tutorials

 References

 Full Version in PDF/EPUB