Dumping HTTP Response with Cookies

This section provides a tutorial example on how to dump the entire HTTP response received from the JSP server to review cookies included in the HTTP headers.

For those of you who want to know how cookies are included in the HTTP response, I have the following Java program to dump HTTP response, HttpRequestGet.java:

/**
 * HttpRequestGet.java
 * Copyright (c) 2012, HerongYang.com, All Rights Reserved. 
 */
import java.io.*;
import java.net.*;
public class HttpRequestGet {
   public static void main(String[] args) {
      String path = "/index.html";
      int port = 80;
      String host = "localhost";
      if (args.length > 0) path = args[0];
      if (args.length > 1) port
         = Integer.valueOf(args[1]).intValue();
      if (args.length > 2) host = args[2];
      String result = "";
      try {
         Socket c = new Socket(host,port);
         BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
            c.getOutputStream()));
         BufferedReader r = new BufferedReader(new InputStreamReader(
            c.getInputStream()));
         String m = "GET "+ path + " HTTP/1.0";
         w.write(m,0,m.length());
         w.newLine();
         w.newLine();
         w.flush();
         while ((m=r.readLine())!= null) {
            System.out.println(m);
         }
         w.close();
         r.close();
         c.close();
      } catch (IOException e) {
         System.err.println(e.toString());
      }
   }
}

Here is simple JSP page that set cookies in different ways, CookieDump.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!--
 - CookieDump.jspx
 - Copyright (c) 2012, 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 temporary cookie
   out.println("<b>Temporary cookie:</b><br/>");
   Cookie c = new Cookie("Language","English");
   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 persisted cookie
   out.println("<b>Persisted cookie:</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/>");

// Setting a temporary cookie with specified properties
   out.println("<b>Temporary cookie with domain defined:</b><br/>");
   c = new Cookie("Password","top_secret");
   c.setDomain("some.com");
   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 persisted cookie with specified properties
   out.println("<b>Persisted cookie with domain defined:</b><br/>");
   c = new Cookie("Login","herong_yang");
   c.setMaxAge(3*24*60*60);
   c.setDomain("some.com");
   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/>");
]]></jsp:scriptlet>
</p>
</body></html>
</jsp:root>

Now install CookieDump.jspx in tomcat default application directory. Then run HttpRequestGet.java:

>java HttpRequestGet /CookieDump.jsp 8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=5CE3CB3B1DC0BA0C797F389631B830AA; Path=/; 
   HttpOnly
Set-Cookie: Language=English
Set-Cookie: User="Herong Yang"; Version=1; Max-Age=259200; 
   Expires=timestamp
Set-Cookie: Password=top_secret; Domain=some.com
Set-Cookie: Login=herong_yang; Domain=some.com; Expires=timestamp
Content-Type: text/html;charset=UTF-8
Content-Length: 665
Connection: close

<html><body><p><b>Temporary cookie:</b><br/>
Name: Language<br/>
Value: English<br/>
Domain: null<br/>
Path: null<br/>
MaxAge: -1<br/>
Version: 0<br/>
<b>Persisted cookie:</b><br/>
Name: User<br/>
Value: Herong Yang<br/>
Domain: null<br/>
Path: null<br/>
MaxAge: 259200<br/>
Version: 0<br/>
<b>Temporary cookie with domain defined:</b><br/>
Name: Password<br/>
Value: top_secret<br/>
Domain: some.com<br/>
Path: null<br/>
MaxAge: -1<br/>
Version: 0<br/>
<b>Persisted cookie with domain defined:</b><br/>
Name: Login<br/>
Value: herong_yang<br/>
Domain: some.com<br/>
Path: null<br/>
MaxAge: 259200<br/>
Version: 0<br/>
</p></body></html>

As you can see, these are 5 cookies included in the HTTP response header section. The first one is added by the JSP server. The other 4 are added by my JSP page.

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 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

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

 Outdated Tutorials

 References

 PDF Printing Version