Returning non-HTML Response Body

This section provides a tutorial example on how to return back non-HTML response body and set HTTP response header lines to match the data type and size of the response body.

Sometimes, you may want to send back information in the entity body that are not in the HTML format, for example, a PDF document, or MS Word Document. In this case, we have to set Content_Type, Content_Length and other header lines carefully to provide correct information about the entity body for the client program.

Here is a sample JSP page to show you how to set header lines for different types of data in the entity body.

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- GetFile.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page session="false" import="java.io.*" />
<jsp:scriptlet>
   String p = request.getQueryString();
   boolean ok = true;
   ok = p!=null;
   if (ok) {
      if (p.indexOf(".html")>-1) {
         response.setContentType("text/html");
      } else if (p.indexOf(".gif")>-1) {
         response.setContentType("image/gif");
      } else if (p.indexOf(".pdf")>-1) {
         response.setContentType("application/pdf");
      } else if (p.indexOf(".doc")>-1) {
         response.setContentType("application/msword");
      } else {
         ok = false;
      }
   }
   if (ok) {
      try {
         String v = application.getRealPath(p);
         int l = (int) new File(v).length();
         response.setContentLength(l);
         byte[] b = new byte[l];
         FileInputStream f = new FileInputStream(v);
         f.read(b);
         ServletOutputStream o = response.getOutputStream();
         o.write(b,0,l);
         o.flush();
         o.close();
         f.close();
      } catch (Exception e) {
         ok = false;
      }
   }
   if (!ok) {
      response.sendError(HttpServletResponse.SC_BAD_REQUEST);
   }
 </jsp:scriptlet>
</jsp:root>

Ideas used in this page:

Now you put some data files with different file name extensions into default application root directory: \local\apache-tomcat-7.0.32\webapps\ROOT, then test my tutorial JSP page:

1. Use IE (Internet Explorer) to request: http://localhost:8080/GetFile.jsp?hello.html, you should see the hello message properly displayed as HTML document.

2. Use IE to request: http://localhost:8080/GetFile.jsp?dot.gif, you should see a tiny dot displayed as an image.

3. Use IE to request: http://localhost:8080/GetFile.jsp?hello.pdf, you should see IE calling Adobe Reader to display the hello message as a PDF document.

4. Use IE to request: http://localhost:8080/GetFile.jsp?hello.doc, you should see IE calling MS Word to display the hello message as Word document. Of course, you have prepare such a Word document and put it on Tomcat server in order to do this test.

5. Use IE to request: http://localhost:8080/GetFile.jsp?any.txt, you should see IE displaying an error message. The reason is, of course, that the requested file doesn't exist.

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