|
As described above, the HTTP response status line consists of
an HTTP version, a status code, and an associated message.
Since the message is directly associated with the status code and
the HTTP version is determined by the server, all the servlet
needs to do is to set the status code. The way to do that is
by the setStatus method of HttpServletResponse.
The setStatus
method takes an int (the status code) as an argument, but instead
of using explicit numbers, it is clearer and more reliable to use
the constants defined in HttpServletResponse. The name of each
constant is derived from the standard HTTP 1.1 message for each constant,
all uppercase with a prefix of SC (for Status Code) and spaces
changed to underscores. Thus, since the message for 404 is
Not Found, the equivalent constant in HttpServletResponse is
SC_NOT_FOUND. There are two exceptions however. For some odd reason
the constant for code 302 is derived from the HTTP 1.0 message, not the
HTTP 1.1 message, and the constant for code 307 is missing altogether.
Setting the status code does not always mean that you don't
need to return a document. For example, although most servers will generate
a small "File Not Found" message for 404 responses, a servlet might want
to customize this response. However, if you do this, you need to be sure
to call response.setStatus before sending any content
via the PrintWriter.
Although the general method of setting status codes is simply to call
response.setStatus(int), there are two common cases where a shortcut method
in HttpServletResponse is provided. The sendError
method generates a 404 response along with
a short message formatted inside an HTML document.
And the sendRedirect method generates a 302 response along with a
Location header indicating the URL of the new document.
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
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.
|