JSP Tutorials - Herong's Tutorial Examples - Version 4.03, by Dr. Herong Yang
Response Header Lines Controlled by response Object
This section provides a tutorial example on how to use methods provided by the 'response' object to control HTTP response header line 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 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.jspx, uses the special methods, response.setContentType() and setContentLength(), to set Content_Type and Content_Length header lines:
<?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <!-- SetContentType.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <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>
Then view response header lines using my test program, HttpRequestGet.java, presented in the previous section:
C:\>java HttpRequestGet /SetContentType.jspx 8080 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=E13483CEC69A621A3341AB826B4342A2; Path=/; HttpOnly Content-Type: text/html;charset=UTF-8 Content-Length: 38 Date: 02:37:36 GMT Connection: close <html><body>Hello world!</body></html>
The output looks good.
In the second example, SetHeader.jspx, I was trying to use generic methods to set various header lines:
<?xml version="1.0"?> <?xml version="1.0"?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"> <!-- SetHeader.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <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>
Then view response header lines:
C:\>java HttpRequestGet /SetHeader.jspx 8080 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=F536266C7518B697FC940122E8D1A44A; Path=/; HttpOnly Content_Type: text/html Content_Length: 38 Last-Modified: 02:47:55 GMT Author: Herong Yang Author: Joe Wang Content-Type: text/xml;charset=UTF-8 Content-Length: 38 Date: 02:47:55 GMT Connection: close <html><body>Hello world!</body></html>
Note that:
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="2.1"> <!-- SetHeaderRevised.jspx - Copyright (c) 2012, HerongYang.com, All Rights Reserved. --> <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>
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)