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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Programming SimpleTag Handlers<br />

266<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

...<br />

<br />

<strong>The</strong> template is instantiated by the Dispatcher servlet. Dispatcher first gets the requested<br />

screen. Dispatcher performs business logic and updates model objects based on the requested<br />

screen. For example, if the requested screen is /bookcatalog, Dispatcher determines whether<br />

a book is being added to the cart based on the value of the Add request parameter. It sets the<br />

price of the book if it’s on sale, and then adds the book to the cart. Finally, the servlet dispatches<br />

the request to template.jsp:<br />

public class Dispatcher extends HttpServlet {<br />

@Resource<br />

UserTransaction utx;<br />

public void doGet(HttpServletRequest request,<br />

HttpServletResponse response) {<br />

String bookId = null;<br />

Book book = null;<br />

String clear = null;<br />

BookDBAO bookDBAO =<br />

(BookDBAO)getServletContext().<br />

getAttribute("bookDBAO");<br />

HttpSession session = request.getSession();<br />

String selectedScreen = request.getServletPath();<br />

ShoppingCart cart = (ShoppingCart)session.<br />

getAttribute("cart");<br />

if (cart == null) {<br />

cart = new ShoppingCart();<br />

session.setAttribute("cart", cart);<br />

}<br />

if (selectedScreen.equals("/bookcatalog")) {<br />

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

if (!bookId.equals("")) {<br />

try {<br />

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

if ( book.getOnSale() ) {<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!