JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Controlling HTTP Response Header Lines

Part:   1  2  3  4  5  6  7 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

Controlling Response Header Lines

When a JSP page is requested, the response header lines will be created by the JSP server. But you can indirectly control some header lines in three 3 different ways:

1. Using a directive element to set the entity header line: Content_Type, as shown in the following example:

<jsp:directive.page contentType="text/html"/>

2. Using special methods on the "response" object, as defined by the javax.servlet.ServletResponse interface, to set the entity header lines: Content_Type and Content_Length as shown in the following example:

   response.setContentType("text/html");
   responee.setContentLength(909);

3. Using generic methods on the "response" object, as defined by the javax.servlet.http.HttpServletResponse interface, to add or set any response header lines as shown in the following example:

   response.setHeader("Content_Type", "text/html");
   response.setIntHeader("Content_Length", 909);
   response.setDateHeader("Last-Modified", System.currentTimeMillis());
   response.addHeader("Content_Type", "charset=ISO-8859-1");

Viewing Response Header Lines

When the client program receives the HTTP response, it will look at the header lines first. Based on the information contained in the header lines, the client program will decide what to do with the actual response data in the entity body.

If you use a Web browser as a HTTP client program, it will process the data in the entity body differently depending on mainly the "Content_Type" entity header line: displaying the data as it is, rendering the data as a HTML document and displaying the resulting information, or passing the data to other registered programs to handle it.

Once the Web browser finishes processing the entity body, you can get some limited information from the header lines. For example, you can click the right mouse button and select the properties command on Internet Explorer, it will display some general properties about this response in a pop up window. The properties displayed are not always identical to the response header lines. The "Modified" property is probably identical to the "Last_Modified" entity header line. The "Type" property is sometime related to the "Content_Type" entity header line, and sometimes related to server side resource that generated the response. For example, if you use Internet Explorer to request hello.jsp from a JSP Web server, and view the page properties, you will see "JavaServer Page" in the "Type" property. But the "Content_Type" header line received from this JSP page is "text/html".

How to view all the header lines received in the HTTP response? I couldn't find any existing tools to do this. So wrote the following program to dump the entire response including all header lines received from a Web server:

/**
 * HttpRequestGet.java
 * Copyright (c) 2002 by Dr. Herong Yang. 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());
      }
   }
}

(Continued on next part...)

Part:   1  2  3  4  5  6  7 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Controlling HTTP Response Header Lines