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

Response Header Lines Affected by jsp:directive.page Elements

As I mentioned earlier, the first way to control the response header lines is to use "jsp:directive.page" elements. Let me use the following 3 example JSP pages to show you how to do this.

Copy the first example JSP page, hello.jsp, to Tomcat server:

<html><body>
<% out.println("Hello world!"); %>
</body></html>

Then obtain the response with "java HttpRequestGet /hello.jsp 8080":

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=4BEF55D47FC7A80A75A97082756B772E; Path=/
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 44
Date: Sat, 23 Mar 2003 21:48:54 GMT
Server: Apache Coyote/1.0
Connection: close

<html><body>
Hello world!

</body></html>

hello.jsp was written in an HTML format with embedded JSP statements, so Tomcat decided to set Content_Type to "text/html;charset=ISO-8859-1", which is perfectly ok.

Copy the second example JSP page, hello_xml.jsp, Tomcat server:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- hello_xml.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Then obtain the response with "java HttpRequestGet /hello_xml.jsp 8080":

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=94F75D820DA1B0BA6023704C5D2E665C; Path=/
Content-Type: text/xml;charset=UTF-8
Content-Length: 40
Date: Sat, 23 Mar 2003 21:59:03 GMT
Server: Apache Coyote/1.0
Connection: close

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

This time, hello_xml.jsp was written in XML format, so Tomcat decided to set Content_Type to "text/xml;charset=UTF-8". This is not right, because it doesn't match the entity body. If you use Internet Explorer to request this JSP page, the entity body will not be rendered as XML data.

In the third example JSP page, hello_xml_html.jsp, I used the jsp:directive.page element to correct the problem in the second example:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- hello_xml_html.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Obtain the response with "java HttpRequestGet /hello_xml_html.jsp 8080":

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=4B6E955411EBC4536206978E3B498B50; Path=/
Content-Type: text/html;charset=UTF-8
Content-Length: 40
Date: Sat, 23 Mar 2003 22:11:40 GMT
Server: Apache Coyote/1.0
Connection: close

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

As you can see from the response, the attribute "contentType" in the "jsp:directive.page" changed the "Content_Type" header line. Note that the "charset" portion was not changed, because no value was given in the "contentType" attribute.

(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