JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Controlling HTTP Response Header Lines

Part:   1  2  3  4  5  6  7 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

Generating Non-HTML Entity 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="1.2">
<!-- GetFile.jsp
     Copyright (c) 2002 by Dr. Herong Yang
-->
<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 {
         int l = (int) new File(p).length();
         response.setContentLength(l);
         byte[] b = new byte[l];
         FileInputStream f = new FileInputStream(p);
         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:

  • The objective of this page is to send back the content of the requested file in entity body, and set the Content_Type and Content_Length header lines correctly.
  • The requested file name is given in the query string of the HTTP request.
  • The extension of the requested file name is checked to determine the Content_Type header line.
  • Then the requested file size is checked to set the Content_Length header line.
  • Then the requested file is opened, and the content is copied to output stream of the response object as a byte array.
  • If any thing goes wrong, an error status code is send to the response.

Now let's see how this page works.

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.

(Continued on next part...)

Part:   1  2  3  4  5  6  7 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Controlling HTTP Response Header Lines