|
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
Dispatcherservlet 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
orderFormdisplays 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. TheDispatcherservlet updates the values in the shopping cart, which are then redisplayed byorderForm. When the order is complete, the customer proceeds to thecheckoutFormpage by clicking the Checkout link.checkoutForm
checkoutFormis used to collect delivery and billing information from the customer. When the Submit button is clicked, the request is posted to thecheckoutAckpage. However, the request is first handled by theDispatcher, which invokes thevalidatemethod ofcheckoutFormBean. If the validation does not succeed, the requested page is reset tocheckoutForm, with error notifications in each invalid field. If the validation succeeds,checkoutFormBeansubmits suborders to each supplier and stores the result in the request-scopedOrderConfirmationsJavaBeans component, and control is passed tocheckoutAck.checkoutAck
checkoutAcksimply displays the contents of theOrderConfirmationsJavaBeans component, which is a list of the suborders that constitute an order and the ship dates of each suborder.JavaBeans Components
RetailPriceList
RetailPriceListis 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 byCheckoutFormBeanwhen it constructs the suborders dispatched to coffee suppliers.
RetailPriceListfirst 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,JAXRQueryByNameconnects to the registry server and searches for coffee suppliers registered with the nameJAXRPCCoffeeSupplierin theexecuteQuerymethod. The method returns a collection of organizations that contain services. Each service is accessible via a service binding or URL.RetailPriceListmakes a JAX-RPC call to each URL.ShoppingCart
ShoppingCartis a list of shopping cart items. AShoppingCartItemcontains a retail price item, the number of pounds of that item, and the total price for that item.OrderConfirmations
OrderConfirmationsis a list of order confirmation objects. AnOrderConfirmationcontains order and confirmation objects, as discussed in Service Interface.CheckoutFormBean
CheckoutFormBeanchecks the completeness of information entered intocheckoutForm. If the information is incomplete, the bean populates error messages, andDispatcherredisplayscheckoutFormwith 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
RetailPriceListServletresponds to requests to reload the price list via the URL/loadPriceList. It simply creates a newRetailPriceListand 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
adminrole.
|
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.