The HttpServletResponse
contains methods for setting the response code and headers for the response. There is also a method for issuing a redirect command to the browser. And finally, there is a ServletOutputStream
that may be used to write content back to the browser.
Setting Response Codes
The response code for a request is a numeric value that represents the status of the response. For example, 200 represents a successful response, 404 represents a file not found, etc. The setStatus()
methods can be used to set the response code. HttpServletResponse
defines a number of constants for the various codes - SC_OK
for 200, SC_NOT_FOUND
for 404, etc.
By default, a response is automatically set with a response code of SC_OK
. The response code may be changed.
Setting Headers
Headers for the response may be set by calling setHeader
, specifying the name and value of the header to be set. If you want to set more than one HTTP header with the same name, you can call addHeader
, addDateHeader
, or addIntHeader
. You might want to do this if, for example, you wanted to set more than one cookie in a single response.
Sending Response Codes and Headers
The response code and headers may not be sent immediately upon calling setStatus
or setHeader
. Typically, the response code and headers are not committed until something is actually written to the ServletOutputStream
. A call to the ServletResponse.isCommitted ()
method will let you know whether the response codes and headers have been sent. If nothing is ever written to the ServletOutputStream
, then the response code and headers are committed when the request is finished.
You should not call setHeader
or setStatus
after you have written something to the ServletOutputStream
since the response may have already been committed.
A couple of other methods may cause the response code and headers to be sent immediately. Calling sendError
instead of setStatus
sets the status code and immediately writes the response code and any headers set up to that point. Calling sendRedirect
or sendLocalRedirect
will have the same effect.
Sending Redirects
The sendRedirect
method is used to issue a redirect to the browser, causing the browser to issue a request to the specified URL. The URL passed to sendRedirect
must be an absolute URL – it must include protocol, machine, full path, etc.
If you are redirecting to another page on the same site, then you should call sendLocalRedirect
instead of sendRedirect
. Unlike sendRedirect
, the sendLocalRedirect
method allows you to specify a relative URL, such as errors/LoginError.jsp
. The sendLocalRedirect
method also includes session information in the location URL, which is required to maintain a user’s session across a redirect.
After calling sendRedirect
or sendLocalRedirect
, no other operations should be performed on the response. Note that if you use response.sendRedirect()
or response.sendLocalRedirect()
calls, they must be made before any content has been output to the response.getOutputStream()
. Once you’ve sent content to the output stream, the response headers will already have been sent and it is no longer possible to modify the response headers to perform a redirect.
This means that you cannot have any content, including any white space, in a Java Server Page before the redirect call is performed. White space is treated as content of the page unless it is between <%
and %>
tags or between <dsp:droplet>
and </dsp:droplet>
tags (and not in an <dsp:oparam>
tag).
Here is an example of a redirect that will not work, because it includes white space in the <dsp:oparam>
tag before the <%
tag:
------ top of the page: <dsp:droplet name="/atg/dynamo/droplet/Switch"> <dsp:param bean="FormHandler.shouldRedirect" name="value"/> <dsp:oparam name="true"> <% ServletUtil.getDynamoResponse(request,response).sendLocalRedirect ("/error.jsp", request); %> </dsp:oparam> </dsp:droplet>
Here is the same example coded so that it will work:
------ top of the page: <dsp:droplet name="/atg/dynamo/droplet/Switch"> <dsp:param bean="FormHandler.shouldRedirect" name="value"/> <dsp:oparam name="true"><% ServletUtil.getDynamoResponse(request,response). sendLocalRedirect ("/error.jsp", request); %> </dsp:oparam> </dsp:droplet>
ServletOutputStream
The ServletOutputStream
is obtained by calling getOutputStream
on the HttpServletResponse
. The ServletOutputStream
is a subclass of OutputStream
that contains a number of convenient print
and println
methods.
Data written to the ServletOutputStream
goes straight back to the browser. In addition, the first data written to the stream causes the response code and headers to be sent out, which means that the headers cannot be changed once data has been written to the ServletOutputStream
. The ServletOutputStream
cannot be used to print headers, response codes, or redirect commands. These must be performed by using the appropriate HttpServletResponse
methods.
In the servlet pipeline, all servlets in the pipeline will generally share the same ServletOutputStream
. So if one servlet prints something to the stream, then the next servlet in the pipeline prints something to the stream, both outputs will appear in the order in which they were printed.
Note that this is different from the servlet chaining function provided by some Web servers. In servlet chaining, the output from one servlet becomes the input of the next servlet, and so on. In the servlet pipeline model, the servlets in the pipeline share the same input and output streams, which lead back to the browser.