background image

Constructing Responses

<< Context Path and Servlet Path | Generating HTML Markup >>
<< Context Path and Servlet Path | Generating HTML Markup >>

Constructing Responses

Constructing Responses
A response contains data passed between a server and the client. All responses implement the
ServletResponse
interface. This interface defines methods that allow you to:
Retrieve an output stream to use to send data to the client. To send character data, use the
PrintWriter
returned by the response's getWriter method. To send binary data in a MIME
body response, use the
ServletOutputStream
returned by getOutputStream. To mix binary
and text data (as in a multipart response), use a ServletOutputStream and manage the
character sections manually.
Indicate the content type (for example, text/html) being returned by the response with the
setContentType(String)
method. This method must be called before the response is
committed. A registry of content type names is kept by the Internet Assigned Numbers
Authority (IANA) at
http://www.iana.org/assignments/media-types/
.
Indicate whether to buffer output with the setBufferSize(int) method. By default, any
content written to the output stream is immediately sent to the client. Buffering allows
content to be written before anything is actually sent back to the client, thus providing the
servlet with more time to set appropriate status codes and headers or forward to another
web resource. The method must be called before any content is written or before the
response is committed.
Set localization information such as locale and character encoding. See
Chapter 15,
"Internationalizing and Localizing Web Applications"
for details.
HTTP response objects,
HttpServletResponse
, have fields representing HTTP headers such as
the following:
Status codes, which are used to indicate the reason a request is not satisfied or that a request
has been redirected.
Cookies, which are used to store application-specific information at the client. Sometimes
cookies are used to maintain an identifier for tracking a user's session (see
"Session
Tracking" on page 127
).
In Duke's Bookstore, BookDetailsServlet generates an HTML page that displays information
about a book that the servlet retrieves from a database. The servlet first sets response headers:
the content type of the response and the buffer size. The servlet buffers the page content because
the database access can generate an exception that would cause forwarding to an error page. By
buffering the response, the servlet prevents the client from seeing a concatenation of part of a
Duke's Bookstore page with the error page should an error occur. The doGet method then
retrieves a PrintWriter from the response.
To fill in the response, the servlet first dispatches the request to BannerServlet, which
generates a common banner for all the servlets in the application. This process is discussed in
"Including Other Resources in the Response" on page 122
. Then the servlet retrieves the book
identifier from a request parameter and uses the identifier to retrieve information about the
Writing Service Methods
The Java EE 5 Tutorial · September 2007
112