The HttpServletResponse can perform these tasks:

Set 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, and so on. 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, and so on.

By default, a response is automatically set with a response code of SC_OK. The response code can be changed.

Set Headers

Headers for the response can 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.

Send Response Codes and Headers

The response code and headers might 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 lets you know whether the response codes and headers were sent. If nothing is ever written to the ServletOutputStream, the response code and headers are committed when the request is finished.

You should not call setHeader or setStatus after you write something to the ServletOutputStream as the response might already be committed.

A couple of other methods can 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 has the same effect.

Send 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, and so on.

If you are redirecting to another page on the same site, you should call sendLocalRedirect instead of sendRedirect. Unlike sendRedirect, the sendLocalRedirect method lets you 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. If you use response.sendRedirect() or response.sendLocalRedirect() calls, they must be made before any content has been output to the response.getOutputStream(). After you send content to the output stream, the response headers are already 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 JSP 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 does 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 does 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>
Set 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 after 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 generally share the same ServletOutputStream. So if one servlet prints something to the stream, the next servlet in the pipeline prints something to the stream, both outputs appear in the order they were printed.

Note: 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.

 
loading table of contents...