Java web development shopping cart function implementation, java shopping cart

Source: Internet
Author: User

Java web development shopping cart function implementation, java shopping cart

In order to facilitate your future review, write carefully and record your own growth .....

Since it is a shopping cart, the precondition is that a series of products are required first, that is, to create an entity. Here a commodity table is created,

Display on the browser by querying

The basic display is ready. Now we are playing a major role, Servlet

When you click "add to shopping cart", the access Servlet

Shopping Cart code

1 package com. servlet; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 import java. util. hashMap; 6 import java. util. map; 7 8 import javax. servlet. servletException; 9 import javax. servlet. http. httpServlet; 10 import javax. servlet. http. httpServletRequest; 11 import javax. servlet. http. httpServletResponse; 12 13 import com. dao. goodsDAO; 14 import com. entity. goods; 15 import com. entity. goodsItem; 16 17 public class PutCarServlet extends HttpServlet {18 19 public void doGet (HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException {21 22 response. setContentType ("text/html"); 23 PrintWriter out = response. getWriter (); 24 25 doPost (request, response); 26} 27 28 public void doPost (HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException {30 31 response. setContentType ("text/html"); 32 PrintWriter out = response. getWriter (); 33 // get id 34 String id = request. getParameter ("goodsID"); 35 36 // obtain all information of the commodity object by serial number 37 GoodsDAO dao = new GoodsDAO (); 38 Goods g = dao. getGoodsByID (id); 39 // put the item into the shopping cart 40 41 // map set is the shopping cart 42 // map <key, value> item number as key item as value 43 44 // 1. determine whether a shopping cart exists. 45 // The shopping cart is placed in the session. 46 // The shopping cart is retrieved from the session. 47 Map <String, GoodsItem> gwc = (Map <String, GoodsItem>) request. getSession (). getAttribute ("gwc"); 48 // determine whether 49 if (gwc = null) {50 // create a shopping cart 51 gwc = new HashMap <String, GoodsItem> (); 52} 53 54 // Add the item to the shopping cart 55 // put (item number, item) add data to the gwc set 56 // you want to check whether the item already exists in the shopping cart 57 // to put it bluntly, match whether such item exists in the gwc set = set match whether there is such a product number key58 59 // determine whether there is a product number key 60 61 if (gwc. containsKey (id )) {62 // 63 // set quantity + 164 65 // obtain the value 66 through the key // The key is the product number value. The item contains the product object information and quantity information. 67 GoodsItem spx = gwc. get (id); 68 // obtain the original number of 69 int yldsl = spx. getCount (); 70 // In the original number + 171 gwc. get (id ). setCount (yldsl + 1); 72 73 // gwc. get (id ). setCount (gwc. get (id ). getCount () + 1); 74 75} else {76 // No 77 // create a new item quantity of 178 GoodsItem gi = new GoodsItem (g, 1 ); 79 80 // add this item to gwc81 gwc. put (id, gi); 82} 83 84 // put the cart in session85 request. getSession (). setAttribute ("gwc", gwc); 86 87 // continue shopping 88 response. sendRedirect ("index. jsp "); 89} 90 91}

Execution result:

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.