Home  >  Article  >  Java  >  Java web development to implement shopping cart function

Java web development to implement shopping cart function

高洛峰
高洛峰Original
2016-12-17 14:35:481551browse

In order to facilitate my own review in the future, I wrote more carefully and recorded my growth.
Since we are making a shopping cart, the prerequisite is that we first need a series of products, that is, we need to build an entity. Here we build a product table,

java web开发之实现购物车功能

and display it on the browser through query

java web开发之实现购物车功能

Basic display Already done, now enter our highlight, Servlet
When you click to put it in the shopping cart, you will access the Servlet

java web开发之实现购物车功能

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();
  //得到编号
  String id = request.getParameter("goodsID");
   
  //通过编号得到商品对象的所有信息
  GoodsDAO dao = new GoodsDAO();
  Goods g = dao.getGoodsByID(id);
  //将商品放入购物车
   
  //map集合 就是购物车
  // map<键,值> 商品编号作为键 商品项作为值
   
  //1.判断是否存在购物车
  //购物车是放在session中的
  //从session去取购物车
  Map<String,GoodsItem> gwc = (Map<String,GoodsItem>)request.getSession().getAttribute("gwc");
  //判断是否存在
  if(gwc==null){
   //创建购物车
    gwc = new HashMap<String, GoodsItem>();
  }
   
  //将商品项放入购物车
  //put(商品编号,商品项) 向gwc集合中添加数据
  //你要想 购物车中是否已存在该商品
  // 说白了 就是在gwc集合中去匹配是否存在这样一个商品项 ==》去集合中匹配是否存在这样一个商品编号的key
   
  //判断是否存在商品编号的键
  
  if(gwc.containsKey(id)){
   //存在
   //设置数量+1
    
   //通过键 获得值
   //键为商品编号 值为商品项 商品项里面包含商品对象信息 和数量信息
   GoodsItem spx = gwc.get(id);
   //得到原来的数量
   int yldsl = spx.getCount();
   //在原来的数量上+1
   gwc.get(id).setCount(yldsl+1);
    
//    gwc.get(id).setCount(gwc.get(id).getCount()+1) ;
    
  }else{
   //不存在
   //创建一个新的商品项 数量为1
   GoodsItem gi = new GoodsItem(g, 1);
    
   //将此商品项放入gwc
   gwc.put(id, gi);
  }
   
  //将购物车放入session
  request.getSession().setAttribute("gwc", gwc);
   
  //继续购物
  response.sendRedirect("index.jsp");
 }
 
}

Execution results:

java web开发之实现购物车功能



More For articles related to implementing the shopping cart function in java web development, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn