Response Header Lines Controlled by "page" Directive

This section provides a tutorial example on how to use 'page contentType' directive elements to control 'Content_Type' HTTP response header line in JSP pages.

As I mentioned earlier, the first way to control the response header lines is to use "page contentType" directive 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 view response header lines using my test program, HttpRequestGet.java, presented in the previous section:

C:\>java HttpRequestGet /hello.jsp 8080

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=65379D20FEDBDF3F1DDB0B1A24B011F3; Path=/; 
   HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 56
Date: 02:05:54 GMT
Connection: close

<html><body>
Hello world!

</body></html>

hello.jsp was written in an HTML format with embedded JSP elements, 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.jspx, Tomcat server:

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- hello_xml.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Then view response header lines:

C:\>java HttpRequestGet /hello_xml.jspx 8080

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C1DB138905FB5A38DBB8E4204FA10A1C; Path=/; 
   HttpOnly
Content-Type: text/xml;charset=UTF-8
Content-Length: 40
Date: 02:25:13 GMT
Connection: close

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

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

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

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- hello_xml_html.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:scriptlet>out.println("Hello world!");</jsp:scriptlet>
</body></html>
</jsp:root>

Then view response header lines:

C:\>java HttpRequestGet /hello_xml_html.jspx 8080

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=A43CD0F7D69D8869CE4E7DF9E54FA94D; Path=/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Length: 40
Date: 02:29:12 GMT
Connection: close

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

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

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

 JavaBean Objects and "useBean" Action Elements

Managing HTTP Response Header Lines

 What Is HTTP Response?

 HTTP Response Header Lines

 Controlling Response Header Lines

 Viewing Response Header Lines

 Response Header Lines of Static Files

Response Header Lines Controlled by "page" Directive

 Response Header Lines Controlled by response Object

 Accessing File System from JSP Pages

 Returning non-HTML Response Body

 Returning Attachments for Web Download

 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