Sending and Receiving Cookies in JSP Pages

This section provides a tutorial example on how to send cookies to the browser and receive cookies from the browser in JSP pages using response.addCookie() and request.getCookie() methods.

Sending cookies to the browser can be done by the addCookie() method on the response object; while receiving cookies from the browser can be done by the getCookies() method on the request object. Here is program to demonstrate how to use those methods:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<!--
 - CookieTest.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[
   out.println("<b>Cookies received by the server:</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(cookies[i].getName()+": "
            +cookies[i].getValue()+"<br/>");
      }
   }
   out.println("<b>Cookies added by the server:</b><br/>");
   Cookie c = new Cookie("Cookie_"+n,"value");
   out.println(c.getName()+": "+c.getValue()+"<br/>");
   response.addCookie(c);
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>

First I opened this page with IE, I got:

Cookies received by the server:
Cookies added by the server:
Cookie_0: value

Then I clicked the refresh button on the IE window, I got:

Cookies received by the server:
Cookie_0: value
JSESSIONID: 7E33A51C5F05A11647467E1735C5084E
Cookies added by the server:
Cookie_2: value

What happened here was that when I opened the page the first time, the server received no cookie from the browser's request. But my program added one cookie named as "Cookie_0" to the response, and the JSP server also added a cookie named as "JSESSIONID".

When I clicked the refresh button, the browser sent the two cookies back to the server in the request. Then my program added another cookie named as "Cookie_2" in the response.

If I keep clicking the refresh button, more and more cookies would be added to the request and response. But there is a limit. Older version of IE will only take up to 20 cookies from one Web server. IE 8 can take up to 50 cookies from one Web server.

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