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.

ook from the bookstore database. Finally, the servlet generates HTML markup that describes<br />

the book information and then commits the response to the client by calling the close method<br />

on the PrintWriter.<br />

public class BookDetailsServlet extends HttpServlet {<br />

...<br />

public void doGet (HttpServletRequest request,<br />

HttpServletResponse response)<br />

throws ServletException, IOException {<br />

...<br />

// set headers before accessing the Writer<br />

response.setContentType("text/html");<br />

response.setBufferSize(8192);<br />

PrintWriter out = response.getWriter();<br />

}<br />

}<br />

// then write the response<br />

out.println("" +<br />

"+<br />

messages.getString("TitleBookDescription")<br />

+");<br />

// Get the dispatcher; it gets the banner to the user<br />

RequestDispatcher dispatcher =<br />

getServletContext().<br />

getRequestDispatcher("/banner");<br />

if (dispatcher != null)<br />

dispatcher.include(request, response);<br />

// Get the identifier of the book to display<br />

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

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

// and the information about the book<br />

try {<br />

Book bd =<br />

bookDB.getBook(bookId);<br />

...<br />

// Print the information obtained<br />

out.println("" + bd.getTitle() + "" +<br />

...<br />

} catch (BookNotFoundException ex) {<br />

response.resetBuffer();<br />

throw new ServletException(ex);<br />

}<br />

}<br />

out.println("");<br />

out.close();<br />

Writing Service Methods<br />

Chapter 4 • <strong>Java</strong> ServletTechnology 113

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

Saved successfully!

Ooh no, something went wrong!