In order to facilitate your future review, I write carefully and record my growth.
Since it is a shopping cart, the prerequisite is that a series of products are first needed, that is, a entity is built, and a product list is built here.
Show on the browser by query
The basic display has been done, and now we are in the highlight of our Servlet
When clicking to put it in the cart, you will access the Servlet
Shopping cart code
package com.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.dao.GoodsDAO;import com.entity.Goods;import com.entity.GoodsItem;public class PutCarServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Get the number String id = request.getParameter("goodsID"); //Get all information about the product object by number GoodsDAO dao = new GoodsDAO(); Goods g = dao.getGoodsByID(id); //Put the product into the shopping cart//map collection is the shopping cart//map<key, value> The product number is used as the key item as the value //1. Determine whether there is a shopping cart //The shopping cart is placed in the session //The shopping cart is taken from the session Map<String,GoodsItem> gwc = (Map<String,GoodsItem>)request.getSession().getAttribute("gwc"); //Display if(gwc==null){ //Create a shopping cart gwc = new HashMap<String,GoodsItem>(); } //Put the product item into the shopping cart //put(Product number,GoodsItem) Add data to the gwc collection //If you want to see if the product already exists in the shopping cart // To put it bluntly, it is to match whether such a product item exists in the gwc set ==》Match whether such a product number exists in the set //Judge whether there is a key for the product number if(gwc.containsKey(id)){ //Existence//Set quantity +1 //Get the value by key //The key is the product number value is the product item. The product item contains product object information and quantity information GoodsItem spx = gwc.get(id); //Get the original quantity int yldsl = spx.getCount(); //+1 on the original quantity gwc.get(id).setCount(yldsl+1); //gwc.get(id).setCount(gwc.get(id).getCount()+1); }else{ //Not exist//Create a new item to the quantity of 1 GoodsItem gi = new GoodsItem(g, 1); //Put this item into gwc gwc.put(id, gi); } //Put the shopping cart into session request.getSession().setAttribute("gwc", gwc); //Continue shopping response.sendRedirect("index.jsp"); }} Execution results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.