10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Writing Service Methods<br />

Writing Service Methods<br />

110<br />

<strong>The</strong> service provided by a servlet is implemented in the service method of a GenericServlet,<br />

in the doMethod methods (where Method can take the value Get, Delete, Options, Post, Put,or<br />

Trace)ofanHttpServlet object, or in any other protocol-specific methods defined by a class<br />

that implements the Servlet interface. In the rest of this chapter, the term service method is<br />

used for any method in a servlet class that provides a service to a client.<br />

<strong>The</strong> general pattern for a service method is to extract information from the request, access<br />

external resources, and then populate the response based on that information.<br />

For HTTP servlets, the correct procedure for populating the response is to first retrieve an<br />

output stream from the response, then fill in the response headers, and finally write any body<br />

content to the output stream. Response headers must always be set before the response has been<br />

committed. Any attempt to set or add headers after the response has been committed will be<br />

ignored by the web container. <strong>The</strong> next two sections describe how to get information from<br />

requests and generate responses.<br />

Getting Information from Requests<br />

A request contains data passed between a client and the servlet. All requests implement the<br />

ServletRequest interface. This interface defines methods for accessing the following<br />

information:<br />

■ Parameters, which are typically used to convey information between clients and servlets<br />

■ Object-valued attributes, which are typically used to pass information between the servlet<br />

container and a servlet or between collaborating servlets<br />

■ Information about the protocol used to communicate the request and about the client and<br />

server involved in the request<br />

■ Information relevant to localization<br />

For example, in CatalogServlet the identifier of the book that a customer wishes to purchase is<br />

included as a parameter to the request. <strong>The</strong> following code fragment illustrates how to use the<br />

getParameter method to extract the identifier:<br />

String bookId = request.getParameter("Add");<br />

if (bookId != null) {<br />

Book book = bookDB.getBook(bookId);<br />

You can also retrieve an input stream from the request and manually parse the data. To read<br />

character data, use the java.io.BufferedReader object returned by the request’s getReader<br />

method. To read binary data, use the ServletInputStream returned by getInputStream.<br />

HTTP servlets are passed an HTTP request object, HttpServletRequest, which contains the<br />

request URL, HTTP headers, query string, and so on.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!