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 17 to minimize the use of scripting.

> 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 25-1.

> Table 25-1 Model and View Components
> Function | JSP Page | JavaBeans Component
> --- | --- | ---
> Update order data | `orderForm` | `ShoppingCart`
> Update delivery and billing data | `checkoutForm` | `CheckoutFormBean`
> Display order confirmation | `checkoutAck` | `OrderConfirmations`

> ### 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. Each time the customer changes the coffee amounts and clicks the Update button, the request is posted back to `orderForm`. The `Dispatcher` servlet updates the values in the shopping cart, which are then redisplayed by `orderForm`. When the order is complete, the customer proceeds to the `checkoutForm` page by clicking the Checkout link.

> #### checkoutForm

> `checkoutForm` is used to collect delivery and billing information for the customer. When the Submit button is clicked, the request is posted to the `checkoutAck` page. However, the request is first handled by the `Dispatcher`, which invokes the `validate` method of `checkoutFormBean`. If the validation does not succeed, the requested page is reset to `checkoutForm`, with error notifications in each invalid field. If the validation succeeds, `checkoutFormBean` submits suborders to each distributor and stores the result in the request-scoped `OrderConfirmations` JavaBeans component and control is passed to `checkoutAck`.

> #### checkoutAck

> `checkoutAck` simply displays the contents of the `OrderConfirmations` JavaBeans component, which is a list of the suborders comprising 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 distributor. This data is used for two purposes: it contains the price list presented to the end user and is used by `CheckoutFormBean` when it constructs the suborders dispatched to coffee distributors.

> It 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 distributors registered with the name `JAXRPCCoffeeDistributor` in the `executeQuery` method. The method returns a collection of organizations which contain services. Each service is accessible via a service binding or URI. `RetailPriceList` makes a JAX-RPC call to each URI.

> #### ShoppingCart

> `ShoppingCart` is a list of shopping cart items. A `ShoppingCartItem` 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. An `OrderConfirmation` contains order and confirmation objects, already discussed in Service Interface.

> #### CheckoutFormBean

> `CheckoutFormBean` checks the completeness of information entered into `checkoutForm`. If the information is incomplete, the bean populates error messages, and `Dispatcher` redisplays `checkoutForm` with the error messages. If the information is complete, order requests are constructed from the shopping cart and the information supplied to `checkoutForm` and are sent to each distributor. As each confirmation is received, an order confirmation is created and added to `OrderConfirmations`.

>     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.getDistributors().iterator();
>                 d.hasNext(); ) {
>             String distributor = (String)d.next();
>             System.out.println(distributor);
>             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().getDistributor()).
>                         equals(distributor) &&
>                         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 =
>         "http://localhost:8080/saaj-coffee-supplier/orderCoffee";

>                 if (distributor.equals(SAAJOrderURL)) {
>                     OrderRequest or =
>                         new OrderRequest(SAAJOrderURL);
>                     confirmation = or.placeOrder(order);
>                 } else {
>                     OrderCaller ocaller =
>                         new OrderCaller(distributor);
>                     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 new `RetailPriceList` and a new `ShoppingCart`.

> Since this servlet would be used by administrators of the Coffee Break Server, it is a protected Web resource. In order 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 Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.