Java Web Development_ Realization of shopping cart function
I haven't touched the shopping cart before, and I don't know what to do with the shopping cart, so I queried a lot of information and summarized the function realization of the shopping cart.
There are three ways to query the data:
1. Realize shopping cart with cookie;
2. Realize shopping cart with session;
3. Use cookies and database (shopping cart information persistence) to realize shopping cart;
=========================================================================
Analyze the advantages and disadvantages of these three methods:
1. A shopping cart with cookies is not ideal. Imagine that if the browser of the client disables cookies,
This method will miscarry here
2. Save the shopping cart information in the session. This is only available in a session. If the user does not log in, or after logging in, add the shopping cart and close the browser
Or after logging out, all the shopping carts added before have miscarried
3. This is the way I want to say here
Main processes:
A. Data flow before user login: when the user adds a shopping cart to his favorite goods without logging in to the system, we can save the shopping cart information at this time
To cookies, this will involve the addition and modification of cookies; That is, if there is no corresponding cookie in the cookie before, add the cookie.
If there is a corresponding cookie in the cookie, it is necessary to modify the cookie (this involves the user adding shopping cart for the same commodity multiple times).
B. Data flow after the user logs in: after the user logs in, the first thing the system does is to obtain the corresponding cookies. If there are relevant shopping cart cookies, the shopping cart will be deleted
The information is persisted by the corresponding user, either added or modified. (add operation: add the shopping cart corresponding to the user if there is no corresponding information; modify operation: similar,
If the shopping cart information of the corresponding user exists, modify it). After logging in, users can also add shopping carts. However, here, they are not added to cookies, but directly persisted to
In the database. Note: the data after the user logs in are all related to the database.
=========================================================================
Code part:
=========================================================================
Note:
addToShoppingCart() (cashTicket == || cashTicket.getId() == || cashTicket.getId() < 1 write("nullId" } (q == || q == "" q = String.valueOf(1 } Cookie cookies[] = (cookies == || cookies.length < 0 System.out.println("there is no any cookie .." } (getUserInSession() == flag = (c.getName().equals(Conf.IDUONA_CASHTICKET_COOKIE_STARTNAME + Integer oldValue = Integer newValue = Integer.valueOf(oldValue + flag = addCookie(Conf.IDUONA_CASHTICKET_COOKIE_STARTNAME + write("success" } CashTicket cashTicketTemp = ShoppingCart oldShoppingCart = oldShoppingCart.setAmount(oldShoppingCart.getAmount() + write("success" } ShoppingCart shoppingCartTemp = shoppingCartTemp.setCreateTime( write("success" readShoppingCartFromCookie() System.out.println("======================================================" Cookie cookies[] = (cookies == || cookies.length < 0 } System.out.println("haha there are many cookies :" + c.getName() + " " +