Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
6.2 HTTP 1.1 Status Codes and Their Purpose
129
301 (Moved Permanently)
The 301 (
SC_MOVED_PERMANENTLY
) status indicates that the requested
document is elsewhere; the new URL for the document is given in the
Location
response header. Browsers should automatically follow the
link to the new URL.
302 (Found)
This value is similar to 301, except that the URL given by the
Location
header should be interpreted as a temporary replacement, not a perma
nent one. Note: in HTTP 1.0, the message was
Moved Temporarily
instead of
Found
, and the constant in
HttpServletResponse
is
SC_MOVED_TEMPORARILY
, not the expected
SC_FOUND
.
Core Note
The constant representing 302 is
SC_MOVED_TEMPORARILY
, not
SC_FOUND
.
Status code 302 is very useful because browsers automatically follow
the reference to the new URL given in the
Location
response header.
It is so useful, in fact, that there is a special method for it,
sendRedi
rect
. Using
response.sendRedirect(url)
has a couple of advan
tages over using
response.setStatus(response.SC_MOVED_TEMPORARILY)
and
response.setHeader("Location", url)
. First, it is shorter and
easier. Second, with
sendRedirect
, the servlet automatically builds a
page containing the link to show to older browsers that don't automat
ically follow redirects. Finally, with version 2.2 of servlets (the version
in J2EE),
sendRedirect
can handle relative URLs, automatically
translating them into absolute ones. You must use an absolute URL in
version 2.1, however.
If you redirect the user to another page within your own site, you should
pass the URL through the
encodeURL
method of
HttpServletRe
sponse
. Doing so is a simple precaution in case you ever use session
tracking based on URL rewriting. URL rewriting is a way to track users
who have cookies disabled while they are at your site. It is implemented
by adding extra path information to the end of each URL, but the serv
let session tracking API takes care of the details automatically. Session
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved