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...)

Setting Header Lines Directly in JSP Pages

The second way and third way to control the response header lines are related to the build-in response object. One is to use the specialized methods, the other is to use the generic methods. Let me use the following 2 examples to show you how those methods work.

The first example, SetContentType.jsp, uses the special methods to set Content_Type and Content_Length header lines:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> 
<!-- SetContentType.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
 <jsp:scriptlet><![CDATA[
   response.setContentType("text/html");
   String text = "<html><body>Hello world!</body></html>";
   response.setContentLength(text.length());
   out.print(text);
 ]]></jsp:scriptlet>
</jsp:root>

The response:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1771F49508924D74AA8B29BB9AB770C8; Path=/
Content-Type: text/html
Content-Length: 38
Date: Tue, 12 Aug 2003 23:05:34 GMT
Server: Apache Coyote/1.0
Connection: close

<html><body>Hello world!</body></html>

Note that the setContentType() method overrides the Content_Type header line completely, including the charset portion. Here is another calling example, response.setContentType("text/html;charset=UTF-8").

In the second example, SetHeader.jsp, I was trying to use the generic methods to set various header lines:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> 
<!-- SetHeader.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
 <jsp:scriptlet><![CDATA[
   response.setHeader("Content_Type","text/html");
   response.setIntHeader("Content_Length",0);
   String text = "<html><body>Hello world!</body></html>";
   response.setIntHeader("Content_Length",text.length());
   response.setDateHeader("Last-Modified", System.currentTimeMillis());
   response.setHeader("Author", "Herong Yang");
   response.addHeader("Author", "Joe Wang");
   out.print(text);
 ]]></jsp:scriptlet>
</jsp:root>

Here is the response:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=6C081157BFD264C3222FB71728C00B4C; Path=/
Content_Type: text/html
Content_Length: 38
Last-Modified: Sat, 23 Mar 2003 13:46:53 GMT
Author: Herong Yang
Author: Joe Wang
Content-Type: text/xml;charset=UTF-8
Content-Length: 38
Date: Sat, 23 Mar 2003 13:46:53 GMT
Server: Apache Coyote/1.0
Connection: close

<html><body>Hello world!</body></html>

Note that:

  • There are two "Content_Type" header lines, one from my setHeader() call, and one added by Tomcat. My guess is that Tomcat does not recognize the Content_Type header line generated by the addHeader() method. So we have to use setContentType() method to control the Content_Type header line.
  • The same issue also exists on the "Content_Length" header line. We have to use setContentLength() method to control the Content_Length header line.
  • The setIntHeader() method is called twice with the same header line name "Content_Length". The second call overrides the first call.
  • I added two new header lines called "Author".

Here is the revised version of the second example, SetHeaderRevised.jsp:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"> 
<!-- SetHeaderRevised.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
 <jsp:scriptlet><![CDATA[
   response.setContentType("text/html;charset=UTF-8");
   String text = "<html><body>Hello world!</body></html>";
   response.setContentLength(text.length());
   response.setDateHeader("Last-Modified", System.currentTimeMillis());
   response.setHeader("Author", "Herong Yang");
   out.print(text);
 ]]></jsp:scriptlet>
</jsp:root>

(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