JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
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
JSP (JavaServer Pages) Overview
Tomcat 7 Installation on Windows Systems
Syntax of JSP Pages and JSP Documents
JavaBean Objects and "useBean" Action Elements
►Managing HTTP Response Header Lines
Controlling 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
Overview of JSTL (JSP Standard Tag Libraries)