The realization of the function of Java Web Development shopping Cart

Source: Internet
Author: User

In order to facilitate their review later, so write more carefully, record their growth.

Since it is a shopping cart, then the precondition is to first need a series of goods, that is, to build an entity, here a commodity table,

Display on a browser through a query

The basic display has been done, now into our plays, Servlet

When you click into the cart, the servlet will be accessed

Shopping Cart Code

1  PackageCom.servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 ImportJava.util.HashMap;6 ImportJava.util.Map;7 8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A  - ImportCom.dao.GoodsDAO; - ImportCom.entity.Goods; the ImportCom.entity.GoodsItem; -  -  Public classPutcarservletextendsHttpServlet { -  +      Public voiddoget (httpservletrequest request, httpservletresponse response) -             throwsservletexception, IOException { +  AResponse.setcontenttype ("text/html"); atPrintWriter out =Response.getwriter (); -          - DoPost (request, response); -     } -  -      Public voidDoPost (httpservletrequest request, httpservletresponse response) in             throwsservletexception, IOException { -  toResponse.setcontenttype ("text/html"); +PrintWriter out =Response.getwriter (); -         //Get number theString id = request.getparameter ("Goodsid"); *          $         //get all information about a product object by numberPanax NotoginsengGoodsdao DAO =NewGoodsdao (); -Goods g =Dao.getgoodsbyid (ID); the         //put the item into the shopping cart +          A         //The Map collection is the shopping cart the         //map< Key, Value > Item number as key item as value +          -         //1. Determine if there is a shopping cart $         //The shopping cart is placed in the session. $         //pick up the shopping cart from the session -Map<string,goodsitem> GWC = (map<string,goodsitem>) request.getsession (). getattribute ("GWC"); -         //determine if there is the         if(gwc==NULL){ -             //Create a shopping cartWuyiGWC =NewHashmap<string, goodsitem>(); the         } -          Wu         //put items into your shopping cart -         //put (item number, item) add data to the GWC collection About         //You want to see if the item already exists in the shopping cart $         //in the GWC collection to match whether there is such a commodity item = = "Go to the collection to match whether there is such a product number of key -          -         //key to determine if a product number exists -      A         if(Gwc.containskey (id)) { +             //exist the             //Set Number +1 -              $             //get a value by key the             //The key is the item number value for the commodity item item contains the Product object information and the quantity information theGoodsitem SPX =gwc.get (ID); the             //get the original number the             intYLDSL =Spx.getcount (); -             //in the original number of +1 inGwc.get (ID). SetCount (yldsl+1); the              the //gwc.get (ID). SetCount (Gwc.get (ID). GetCount () +1); About              the}Else{ the             //does not exist the             //Create a new item quantity of 1 +Goodsitem GI =NewGoodsitem (g, 1); -              the             //put this item into the GWCBayi gwc.put (id, GI); the         } the          -         //put the cart into session -Request.getsession (). SetAttribute ("GWC", GWC); the          the         //Continue shopping theResponse.sendredirect ("index.jsp"); the     } -  the}

Execution Result:

The realization of the function of Java Web Development shopping Cart

Related Article

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.