Generating the Server
Response: HTTP Status Codes
Example HTTP 1.1 Response
HTTP/1.1
200
OK
Content Type: text/html
...
Changing the status code lets you perform a
number of tasks not otherwise possible
Forward client to another page
Indicate a missing resource
Instruct browser to use cached copy
Set status before sending document
49
Servlets
www.corewebprogramming.com
Setting Status Codes
public void setStatus(int statusCode)
Use a constant for the code, not an explicit int.
Constants are in HttpServletResponse
Names derived from standard message.
E.g., SC_OK, SC_NOT_FOUND, etc.
public void sendError(int code,
String message)
Wraps message inside small HTML document
public void sendRedirect(String url)
Relative URLs permitted in 2.2/2.3
Also sets Location header
50
Servlets
www.corewebprogramming.com
25