assasinC
assasinC

Reputation: 87

Not able to add more than 2 values in cookies

Hi I am new to java servlets. I was trying to implement simple shopping cart problem using cookies. In my web page I ask user to input a product name that she can add to her cart.

Web page: Simple text input (product name) and a Button (add to cart)

Upon clicking the button, the web page displays the cart values.

I am experiencing a strange problem. It works fine for less than 3 values. When I try to add third value, it's only displaying the most recently added two values. For example if I add products a, b and c respectively. It displays b and c only.

Cart Servlet code:

String name = request.getParameter("productname");

Cookie cookie = new Cookie("product", name);
cookie.setMaxAge(100 * 60);     
response.addCookie(cookie);


Cookie[] cookies = request.getCookies();

List<String> names = new ArrayList<String>();
names.add(name);

if (cookies != null) {
    for (Cookie c: cookies) {
        if (c.getName().equals("product")) {
            names.add(c.getValue());
        }
    }
}

PrintWriter writer = response.getWriter();
for (String item: names) {
    writer.println("<h4>" + item + "</h4>");
}

index.jsp:

<form method="post" action="Cart">
 <input type="text" name="productname" value="Product name">
 <input type="submit" value="AddToCart">
</form>

I have spent so many hours to figure out what's wrong in it. but I couldn't find any mistake to explain this strange behavior. Please help.

Upvotes: 0

Views: 1463

Answers (2)

assasinC
assasinC

Reputation: 87

I found that following piece in web.xml doesn't invalidate session even after closing the browser. Thanks to fI12 for suggesting the link of Session Lost when closing the browser.

<session-config>
   <session-timeout>11520</session-timeout>
   <cookie-config>
     <max-age>11520</max-age>
   </cookie-config>
</session-config>

Using cookies for shopping cart is not a scalable solution. Sessions are the way for this.

Upvotes: 0

f1l2
f1l2

Reputation: 491

The behavior came from the way how your client (browser) and server handle cookies with the same name.

"Several cookies might have the same name but different path attributes."

https://docs.oracle.com/javaee/7/api/javax/servlet/http/Cookie.html

So in your case it is basically the same cookie (based on name and path) which is updated constantly.

So what happens in your case: Basically, the browser returns cookies to the servlet by adding fields to HTTP request headers. Assuming that your first product was named a and you are sending your second product with the name b, your cookie-header looks like

Cookie:JSESSIONID=BCC7050AE82AEA1A4B9BED1174424A92; product=a

and your form-data

productname:b

The server receives the request and a new Cookie is created and added to the HttpResponse cookie list. The list containing two cookies named product is correctly printed in your response. Since the servlet sends cookies to the browser by using HTTP response headers and your cookies are the same, only an update takes place.

Set-Cookie:product=b; Expires=Sat, 23-Dec-2017 11:23:53 GMT 

As a consequence the next request only contains the information regarding product b and product a disappears.

Upvotes: 1

Related Questions