Unformatted text preview:

Appendix BAdding to the Shopping CartChecking OutLogging in to Submit the OrderAdding the Order to the Order Table and the Product/Order TableCreating an XML File for the OrderAppendix BThe example shown here is for an on-line store. This one sells scarves, hats, and gloves. It refers to a database with the following table.The index page contains two forms, one to display the products and the other to find a product, given its name. The action attribute for the form that finds a product refers to a Java server page, shown next. JSP was chosen, because when a product is found, the shopper is given the option of adding it to the shopping cart. This involves a second form.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><link rel='stylesheet' type='text/css' href='estyles.css' /><html><head><title> Find Produce JSP. </title></head><body><jsp:useBean id="findBean" scope="session" class="orders.FindBean" /><jsp:setProperty name="findBean" property="*" /><% findBean.processRequest(application); %><% if (findBean.getInStock ()) { %><h4>This item is in stock.<p>Id: <% out.println (findBean.getId()); %> <br/>Name: <% out.println (findBean.getName()); %> <br/>Price: <% out.println (findBean.getPrice()); %></p></h4><h3>Before adding this to your shopping cart, enter the quantity desired.</h3><form method="get" action="addToCart.jsp"><p><input type="text" name="id" value="<%= findBean.getId() %>" size="30" /> ID<br/><input type="text" name="quantity" value="" size="30" /> Quantity</p><p><input type= "submit" value="Add To Cart" /></p></form> <% } else { %><h4>We are sorry, but the product is out of stock.</h4><% } %><hr><p><a href="../estore">Return</a></p></body></html>1The Java bean that goes with the above JSP follows:package orders;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;/* FindBean first gets a connection to the database and then searches for the item requested. If the itemis found, it then checks to see if it is in stock. The results are returned to the Java server page. */public class FindBean{// The connection is stored in the application’s ServletContext.public final String ConnectionKey = "estore.database";private String id, name;private int quantity=0;private double price;private boolean found, inStock;// The accessor methods.2public String getId() {return id;}public String getName() {return name;}public int getQuantity () {return quantity;}public double getPrice() {return price;}public boolean getInStock () {return inStock;}// The only mutator method needed.public void setName (String n) {name = n;}public void processRequest (ServletContext application){try{// Get the database connection from the ServletContext.Connection con = (Connection) application.getAttribute (ConnectionKey);// Create a query and find the product.Statement stmt = con.createStatement ();String query = "Select * From products Where name = '" + name + "'";ResultSet rs = stmt.executeQuery (query);if (rs.next ()){id = rs.getString ("id");name = rs.getString ("name");quantity = rs.getInt ("quantity");price = rs.getDouble ("price");}// If quantity is 0, the product is either not in stock, or the product was not found.if (quantity > 0) inStock = true;else inStock = false;} catch (SQLException e){System.out.println ("SQL Exception");}} // processRequest } // FindBeanAdding to the Shopping CartIf the shopper chooses to add this item to the cart, clicking the button brings up another Java server page, addToCart.jsp.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><link rel='stylesheet' type='text/css' href='estyles.css' /><html><head><title> Add To Cart JSP. </title></head><body><jsp:useBean id="addToCartBean" scope="session" class="orders.AddToCartBean" /><jsp:setProperty name="addToCartBean" property="*" />3<% addToCartBean.processRequest(request, application); if (addToCartBean.getEnoughStock ()) { %><h4>The <%= addToCartBean.getName () %> has been added to your shopping cart.</h4><% } else { %><h4>Sorry, there is not enough of this product in stock. <h4><% } %><h4><a href="../estore/">Return to shopping</a></h4><h4><form method="get" action="../estore/checkout"><input type="submit" value="Check Out" /></form></body></html>The shopping cart contains several things, including the customer’s ID and an ID for the order. It also contains a vector of items. An item for this example only stores the fields in the database.package orders;// Item stores data for a single ordered item.public class Item{private String productId, name;private int quantityOrdered;private double price;public Item (String pd, String n, int q, double p){productId = pd;name = n;quantityOrdered = q;4price = p;} // constructorpublic String getProductId () {return productId;}public String getName () {return name;}public int getQuantityOrdered () {return quantityOrdered;}public double getPrice() {return price;}} // class ItemThe shopping cart maintains a vector of items. It also keeps track of the ID for the order, the customer’s ID, and the running total cost of the order. (Note: In Java 5, Vectors use generics, i.e. the type of the item to be stored in the Vector is provided at compile time.)package orders;import java.util.*; // The Vector class is in java.util.// The Shopping cart is used to store the items a customer wishes to order.public class ShoppingCart{private String orderId, customerId;private Vector<Item> items;private double total = 0;public ShoppingCart (String o, String c){items = new Vector<Item> ();orderId = o;customerId = c;} // constructorpublic int getNoItems () {return items.size ();}public String getOrderId () {return orderId;}public String getCustomerId () {return customerId;}public double getTotal () {return total;}public void addItem (Item item){total += item.getQuantityOrdered () * item.getPrice ();items.addElement (item);} // addItempublic Item getItem (int index) { return items.elementAt (index);}} // class ShoppingCartThe bean that works with addToCart.jsp is AddToCartBean. If this is the first time that the shopper has added something to the cart, a new cart is created. Otherwise, the existing cart is retrieved from the session and the new item is added. package orders;import java.sql.*;5import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;/* AddToCartBean gets the cart from the session, checks to see that there is enough of the item in stock to satisfy the request, and then adds the item to the cart. */public class


View Full Document
Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?