Tutorials : Server-Side Web Applications Using Servlets and JSP :
: Specifying HTTP Response Headers

Contents
Specifying HTTP Response Headers
Common Response Headers and their Meaning
Example: Automatically Reloading Pages as Content Changes

Specifying HTTP Response Headers

1. Overview

A response from a Web server normally consists of a status line, one or more response headers, a blank line, and the document. Setting the HTTP response headers often goes hand in hand with setting the status codes in the status line. For example, several of the "document moved" status codes have an accompanying Location header, and a 401 (Unauthorized) code must include an accompanying WWW-Authenticate header.

However, specifying headers can play a useful role even when no unusual status code is set. Response headers can be used to specify cookies, to supply the modification date (for caching), to instruct the browser to reload the page after a designated interval, to say how long the file is so that persistent HTTP connections can be used, and many other tasks.

The most general way to specify headers is by the setHeader method of HttpServletResponse, which takes two strings: the header name and the header value. Like setting the status codes, this must be done before any document content is sent.

There are also two specialized methods to set headers that contain dates (setDateHeader) and integers (setIntHeader). The first saves you the trouble of translating a Java date in milliseconds since the epoch (as returned by System.currentTimeMillis or the getTime method applied to a Date object) into a GMT time string. The second spares you the minor inconvenience of converting an int to a String.

Rather than setting a header outright, you can add a new header, in case a header with that name already exists. Use addHeader, addDateHeader, and addIntHeader for this. If it really matters to you whether a specific header has already been set, use containsHeader to check.

Finally, HttpServletResponse also supplies a number of convenience methods for specifying common headers.

  • The setContentType method sets the Content-Type header, and is used by the majority of servlets.
  • The setContentLength method sets the Content-Length header, useful if the browser supports persistent (keep-alive) HTTP connections.
  • The addCookie method sets a cookie (there is no corresponding setCookie, since it is normal to have multiple Set-Cookie lines).
  • And, as discussed in the previous section, the sendRedirect method sets the Location header as well as setting the status code to 302.

NEXT


This tutorial is now available as a book: Core Servlets and JavaServer Pages by Marty Hall, published by Sun Microsystems Press. Read all about it at CoreServlets.com


Server-Side Web Applications using Java Servlets versions 2.1/2.2 and JavaServer Pages (JSP) version 1.0: A Tutorial
© 1999-2000 Marty Hall.
All source code freely available for unrestricted use.
Created for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and on-site Java short courses.
Please note that this is a first draft of the tutorial, so please send corrections, comments, and suggestions to me at hall@apl.jhu.edu.
Reprinted with permission from the author. Click here to visit the original version

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.