★ wanayoo — archive 1999 http://java.webdeveloper.com/tutorials/JSP/part08/Nouvelle recherche | Portail wanayoo

100's of firms that help your Web site be found


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Register Domains
Free Barter Account
Business Search
Promote Your Website
Find a Consultant
Web Hosting Specials
Send a Press Release
Website Monitoring
Software Store
Build Flash Online
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:


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

internet.com logo














WebDev News

Microsoft Buys Into SCO Group's Unix

Microsoft Joins Security Firms in Virus-Warning Pact

HP Releases Itanium-based Operating System





IronEye Cache
IronEye Cache from IronGrid, Inc. is a Java tool that easily enables intelligent caching in any JDBC application with no changes to source code.
Download of the Week
X Images
This is an Image Show applet. All images have a URL. If you double-click on an image you go to that url with specified frame.

JB User Poll
As a JavaBoutique Reader what kind of articles do you prefer?
The JavaBoutique Top 15:
1. ChompText
2. PingPong
3. DJPopMenu
4. NavBar
5. RushHour
6. 3DMaze
7. SonaliCalendar
8. Encyclo
9. AnimLetters_anim
10. AScroll2
11. Nibbly
12. Viewer
13. BMI
14. BMMenu
15. imageviewer
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Dell Well Positioned for the New Tech Environment
If technology is becoming a commodity, then Dell may be the best-positioned tech company of them all.

Keeping Tabs on Your Good Name
A new application, Google Alert, offers a new approach for staying up to date with your interests by routinely performing daily Google searches and sending e-mail notifications of new results that appear, complete with the links.

Security Product of the Year -- Norton AntiVirus Corporate Edition
Answer: Yaha, Klez, Lovegate, Bugbear and Backdoor. Question: What are five reasons Datamation readers selected Symantec AntiVirus Corporate Edition 7.6 as the Security Product of the Year?


Copyright 2003 Jupitermedia Corporation All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/