The Mall shopping Cart module implemented by JSP

Source: Internet
Author: User

These two days, in learning JSP, just find a small module to practice practiced hand:

Here is how to implement the Shopping Cart module page effect:

Figure 1. Product Display page through this page for product selection, add to the shopping cart

Figure 2: Shopping Cart page

Figure 3. Item Quantity Setup

OK, not the map first, directly on the code; First look at the document structure of the project (perfectly formed):

The entire project consists of three classes and two JSP pages, each of which is labeled with the following code:

Cart.java

Package Shopping.cart;import Java.util.arraylist;import Java.util.iterator;import Java.util.list;public class Cart { list<cartitem> items = new arraylist<cartitem> ();p ublic list<cartitem> GetItems () {return items;} public void Setitems (list<cartitem> items) {this.items = items;} public void Add (Cartitem ci) {for (iterator<cartitem> iter = Items.iterator (); Iter.hasnext ();) {Cartitem item = Iter.next (); if (Item.getproduct (). GetId () = = Ci.getproduct (). GetId ()) {Item.setcount (Item.getcount () + 1); return;}} Items.Add (CI);} Public double Gettotalprice () {Double d = 0.0;for (iterator<cartitem> it = Items.iterator (); It.hasnext ();) {Cartitem current = It.next ();d + = Current.getproduct (). GetPrice () * Current.getcount (); return D;} public void Deleteitembyid (String productId) {for (iterator<cartitem> iter = Items.iterator (); Iter.hasnext ();) {Cartitem item = Iter.next (); if (Item.getproduct (). GetId (). Equals (ProductId)) {iter.remove (); return;}}}


Cartitem.java

Package Shopping.cart;import Shopping.cart.product;public class Cartitem {private Product product;private int count; public int GetCount () {return count;} public void SetCount (int count) {This.count = count;} Public product getproduct () {return product;} public void Setproduct (product product) {this.product = product;}}


Product.java

Package Shopping.cart;import Java.io.serializable;public class Product implements Serializable {private String id;// Product Identification private string name;//product name private string description;//Product description Private double price;//price public product () {}public P Roduct (string ID, string name, string description, double price) {this.id = Id;this.name = Name;this.description = Descrip Tion;this.price = Price;} public void SetId (String id) {this.id = ID;} public void SetName (String name) {this.name = name;} public void SetDescription (String description) {this.description = description;} public void Setprice (double price) {this.price = Price;} Public String GetId () {return ID;} Public String GetName () {return name;} Public String GetDescription () {return description;} Public double GetPrice () {return price;}}


Here are the two JSP page source code:

showproducts.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%@ page import= "shopping.cart.*"% ><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/";%&GT;&L t;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


buy.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%><%@ page import= "shopping.cart.*"% ><%cart C = (cart) session.getattribute ("cart"), if (c = = null) {c = new cart (); Session.setattribute ("cart", c);} Double totalprice = C.gettotalprice (); Request.setcharacterencoding ("GBK"); String action = request.getparameter ("action"); MAP products = (HASHMAP) session.getattribute ("products"); if (action! = null && Action.trim (). Equals ("Add")) { String id = request.getparameter ("id"); Product P = (product) products.get (ID); Cartitem ci = new Cartitem (); ci.setproduct (P); Ci.setcount (1); C.add (CI);} if (action! = null && Action.trim (). Equals ("delete")) {String id = request.getparameter ("id"); C.deleteitembyid ( ID);} if (action! = null && Action.trim (). Equals ("Update")) {for (int i=0; I<c.getitems (). Size (); i++) {Cartitem CI = c . GetItems (). get (i), int count = Integer.parseint (Request.getparameter ("P" + ci.getproduct (). GetId ())); Ci.setcount ( count);}} %> <%strinG Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <%List<CartItem> items = C.getitems ();%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


Configure the relevant files, after the publication in Tomcat, enter http://localhost:8088/shopCart/ShowProducts.jsp in the browser can enter the product display page, other operations can be completed on the page!

Note: the Tomcatport (8088) I used was changed by myself, assuming that Tomcatport's children's shoes were not changed, and the default port was 8080.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.