The realization of the shopping cart function _java by Java Web Development

Source: Internet
Author: User
Tags java web

In order to facilitate their own later review, so write more carefully, record their growth.
Since it is a shopping cart, then the prerequisite is to first need a series of goods, that is, to build an entity, here built a commodity table,

Display on the browser through a query

The basic show has been done, now into our plays, the Servlet
When clicked into the shopping cart, the servlet is accessed

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 Respo
  NSE) 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 ();
  
  Gets the number String id = request.getparameter ("Goodsid");
  Get all the information of the merchandise object by number Goodsdao DAO = new Goodsdao (); Goods g = DAO.GEtgoodsbyid (ID);
  Put the goods into the shopping cart//map collection is the cart//map< key, Value > Item number as the key commodity item as the value//1. Determine if a shopping cart//cart is in session//from session to fetch cart
  Map<string,goodsitem> GWC = (map<string,goodsitem>) request.getsession (). GetAttribute ("GWC");
  Determine if there is an if (gwc==null) {//create cart GWC = new hashmap<string, goodsitem> (); ///Put items into Cart//put (item number, item) add data to the GWC collection//You want to have the product in the shopping cart//Plainly it is in the GWC set to match whether there is such a commodity = = "Go to the collection to match whether there is such a commodity Number key//To determine if the existence of the item number keys if (Gwc.containskey (ID)) {//presence//Set quantity +1//key to get value//keys for commodity number value for commodity item items contain commodity objects
   Information and quantity information Goodsitem SPX = Gwc.get (ID);
   Get the original quantity int yldsl = Spx.getcount ();
   
In the original quantity +1 Gwc.get (id). SetCount (yldsl+1);
   
  Gwc.get (ID). SetCount (Gwc.get (ID). GetCount () +1);
   
   }else{//does not exist//create a new item quantity of 1 Goodsitem gi = new Goodsitem (g, 1);
  Put this item into the GWC gwc.put (ID, GI);
  
  ///place cart into session request.getsession () setattribute ("GWC", GWC); Continue shopping Response.sendreDirect ("index.jsp");

 }

}

Execution results:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.