Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P13

Số trang: 10      Loại file: pdf      Dung lượng: 269.05 KB      Lượt xem: 8      Lượt tải: 0    
Jamona

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P13:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P13public ShoppingCart() {}/*** Add a new item to the shopping cart.** attributes* itemId - the unique key associated with the item* desc - a text description of the item* price - the unit price for this item* quantity - number of this item to insert into the* shopping cart*/public void addItem(String itemId, String desc, float price, int quantity) { String[] item = {itemId, desc, Float.toString(price), Integer.toString(quantity)}; if (items.containsKey(itemId)) { String[] tmpItem = (String[])items.get(itemId); int tmpQuant = Integer.parseInt(tmpItem[3]); quantity += tmpQuant; tmpItem[3] = Integer.toString(quantity); } else { - 121 - items.put(itemId, item); }}/*** Remove an item from the shopping cart.** attributes* itemId - the unique key associated with the item to be* removed*/public void removeItem(String itemId) { if (items.containsKey(itemId)) { items.remove(itemId); }}/*** Change the quantity of a specific item in the shopping cart.* The item must have previously been added to perform this* function.** attributes* itemId - unique key for the item to be updated* quantity - the new quantity to be stored in the shopping* cart*/public void updateQuantity(String itemId, int quantity) { - 122 - if (items.contains(itemId)) { String[] tmpItem = (String[])items.get(itemId); tmpItem[3] = Integer.toString(quantity); }}/*** Get an Enumeration to the list of items in the shopping cart.*/public Enumeration getEnumeration() {return items.elements();}/*** Get the total cost of all of the items currently in the* shopping cart.*/public float getCost() { Enumeration enum = items.elements(); String[] tmpItem; float totalCost = 0.00f; while (enum.hasMoreElements()) { tmpItem = (String[])enum.nextElement(); totalCost += (Integer.parseInt(tmpItem[3]) * Float.parseFloat(tmpItem[2])); - 123 - } return totalCost; } /** * Get the total number of items currently in the shopping cart. */ public int getNumOfItems() { Enumeration enum = items.elements(); String[] tmpItem; int numOfItems = 0; while (enum.hasMoreElements()) { tmpItem = (String[])enum.nextElement(); numOfItems += Integer.parseInt(tmpItem[3]); } return numOfItems; }}To install this bean, compile it and move the resulting class file to the /purejsp/WEB-INF/classes/ directory.Integrating the Shopping CartNow that we have a shopping cart bean, lets create a JSP that makes use of the cart. The JSP that wehave created to do this is in Listing 13.2.Listing 13.2: AddToShoppingCart.jsp - 124 - DVD Catalog Shopping Cart Quantity: DVD Catalog DescriptionPrice Happy Gilmore $19.95 Waynes World $19.95 Tommy Boy $15.95 Lethal Weapon 4 $19.95 - 125 - Nutty Professor $19.95 There are four areas of this JSP on which you need to focus. The first is the line of code that instantiatesthe ShoppingCart bean. This is done with the standard action. The bean is createdwith an id of cart and has session scope. The following code snippet contains this action: Note You should notice that the cart object has session scope. This is the logical place for a shopping cart to be stored. This is because the session is client specific.The next area you need to examine is the JSP scriptlet section that checks the request for items to add tothe shopping cart. If the scriptlet finds any items in the request, it will add them using theShoppingCart.add() method. The code snippet that contains this scriptlet is listed below:The third section that needs to be looked at is the line that prints the current quantity of the cart object.The value is printed in the JSP using a JSP expression. The line of code that does this is in the followingcode snippet:Shopping Cart Quantity: The final section you need to notice is the form that submits items to be ...

Tài liệu được xem nhiều: