Java does not read data from a cookie

Source: Internet
Author: User

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

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.