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

 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