Download
FAQ History |
API
Search Feedback |
Coffee Break Server
The Coffee Break server uses servlets, JSP pages, and JavaBeans components to dynamically construct HTML pages for consumption by a Web browser client. The JSP pages use the template tag library discussed in A Template Tag Library to achieve a common look and feel among the HTML pages, and many of the JSTL custom tags discussed in Chapter 14.
The Coffee Break server implementation is organized along the Model-View-Controller design pattern. The
Dispatcher
servlet is the controller. It examines the request URL, creates and initializes model JavaBeans components, and dispatches requests to view JSP pages. The JavaBeans components contain the business logic for the application; they call the Web services and perform computations on the data returned from the services. The JSP pages format the data stored in the JavaBeans components. The mapping between JavaBeans components and pages is summarized in Table 35-1.
JSP Pages
orderForm
orderForm
displays the current contents of the shopping cart. The first time the page is requested, the quantities of all the coffees are 0 (zero). Each time the customer changes the coffee amounts and clicks the Update button, the request is posted back toorderForm
. TheDispatcher
servlet updates the values in the shopping cart, which are then redisplayed byorderForm
. When the order is complete, the customer proceeds to thecheckoutForm
page by clicking the Checkout link.checkoutForm
checkoutForm
is used to collect delivery and billing information from the customer. When the Submit button is clicked, the request is posted to thecheckoutAck
page. However, the request is first handled by theDispatcher
, which invokes thevalidate
method ofcheckoutFormBean
. If the validation does not succeed, the requested page is reset tocheckoutForm
, with error notifications in each invalid field. If the validation succeeds,checkoutFormBean
submits suborders to each supplier and stores the result in the request-scopedOrderConfirmations
JavaBeans component, and control is passed tocheckoutAck
.checkoutAck
checkoutAck
simply displays the contents of theOrderConfirmations
JavaBeans component, which is a list of the suborders that constitute an order and the ship dates of each suborder.JavaBeans Components
RetailPriceList
RetailPriceList
is a list of retail price items. A retail price item contains a coffee name, a wholesale price per pound, a retail price per pound, and a supplier. This data is used for two purposes: it contains the price list presented to the end user and is used byCheckoutFormBean
when it constructs the suborders dispatched to coffee suppliers.
RetailPriceList
first performs a JAXR lookup to determine the JAX-RPC service endpoints. It then queries each JAX-RPC service for a coffee price list. Finally it queries the SAAJ service for a price list. The two price lists are combined and a retail price per pound is determined by adding a markup of 35% to the wholesale prices.Discovering the JAX-RPC Service
Instantiated by
RetailPriceList
,JAXRQueryByName
connects to the registry server and searches for coffee suppliers registered with the nameJAXRPCCoffeeSupplier
in theexecuteQuery
method. The method returns a collection of organizations that contain services. Each service is accessible via a service binding or URL.RetailPriceList
makes a JAX-RPC call to each URL.ShoppingCart
ShoppingCart
is a list of shopping cart items. AShoppingCartItem
contains a retail price item, the number of pounds of that item, and the total price for that item.OrderConfirmations
OrderConfirmations
is a list of order confirmation objects. AnOrderConfirmation
contains order and confirmation objects, as discussed in Service Interface.CheckoutFormBean
CheckoutFormBean
checks the completeness of information entered intocheckoutForm
. If the information is incomplete, the bean populates error messages, andDispatcher
redisplayscheckoutForm
with the error messages. If the information is complete, order requests are constructed from the shopping cart and the information supplied tocheckoutForm
, and these orders are sent to each supplier. As each confirmation is received, an order confirmation is created and added toOrderConfirmations
.if (allOk) { String orderId = CCNumber; AddressBean address = new AddressBean(street, city, state, zip); CustomerBean customer = new CustomerBean(firstName, lastName, "(" + areaCode + ") " + phoneNumber, email); for (Iterator d = rpl.getSuppliers().iterator(); d.hasNext(); ) { String supplier = (String)d.next(); System.out.println(supplier); ArrayList lis = new ArrayList(); BigDecimal price = new BigDecimal("0.00"); BigDecimal total = new BigDecimal("0.00"); for (Iterator c = cart.getItems().iterator(); c.hasNext(); ) { ShoppingCartItem sci = (ShoppingCartItem) c.next(); if ((sci.getItem().getSupplier()). equals(supplier) && sci.getPounds().floatValue() > 0) { price = sci.getItem().getWholesalePricePerPound(). multiply(sci.getPounds()); total = total.add(price); LineItemBean li = new LineItemBean( sci.getItem().getCoffeeName(), sci.getPounds(), sci.getItem().getWholesalePricePerPound()); lis.add(li); } } if (!lis.isEmpty()) { OrderBean order = new OrderBean(address, customer, orderId, lis, total); String SAAJOrderURL = URLHelper.getSaajURL() + "/orderCoffee"; if (supplier.equals(SAAJOrderURL)) { OrderRequest or = new OrderRequest(SAAJOrderURL); confirmation = or.placeOrder(order); } else { OrderCaller ocaller = new OrderCaller(supplier); confirmation = ocaller.placeOrder(order); } OrderConfirmation oc = new OrderConfirmation(order, confirmation); ocs.add(oc); } } }RetailPriceListServlet
The
RetailPriceListServlet
responds to requests to reload the price list via the URL/loadPriceList
. It simply creates a newRetailPriceList
and a newShoppingCart
.Because this servlet would be used by administrators of the Coffee Break server, it is a protected Web resource. To load the price list, a user must authenticate (using basic authentication), and the authenticated user must be in the
admin
role.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.