E-commerce Project Shopping Cart module requires users to add items to the shopping cart without login, so the design is as follows:
1. If the user is already logged in when adding a shopping cart, store the cart data in Redis and the database, and the data is consistent and accessible from Redis when the user queries;
2. If the user is not logged in when the shopping cart is added, the shopping cart data is added to the cookie, and the query, modification and deletion are also made in the cookie, and if the user logs in successfully, the shopping cart data in the cookie is persisted to the database and Redis.
When the user is not logged in, add the cart to the cookie with the following code:
The Cookiekey generation rule here is: Goodscart_goodsid_property, where the property is Propertyid:propertyvalueid, and multiple are connected with "_", such as 1:23_2:24_3:25.
To inquire about the shopping cart information from the cookie, the code is as follows:
However, there is only one cookie in the array of queried cookies, that is, Jsesssionid, and there is no information about the shopping cart that I have stored in, and then I see the cookie in the browser, and I find that it exists, but I can't read it.
Solution:
After troubleshooting, it is because of the special characters of the cookie, ":" is a special character in the cookie, parsing error occurs when the index reads from the cookie;
A better solution is to urlencode the cookie before it is written to the client browser, and urldecode the cookie when it is read.
Add to Cart:
Enquiry Cart:
Problem solved perfectly.
Java does not read data from a cookie