1

Recently, I am trying to write shopping cart with Java session. I use List object to store products and then store in the session. When the user change the amount of the product is changed, I use the code below try to update the session. And it does work, cause when I refresh my page the number showing next to the cart icon is changed, but I really don't know how it works. Can anyone help me please ;/ ?
Here's the code when the user cancel a sprcific product, and I just remove from the list:

List<OrderProduct> orderProductList = (ArrayList<OrderProduct>)session.getAttribute("orderProductList");
for (int i = 0; i < orderProductList.size(); i ++) {
        if (orderProductList.get(i).getProduct().getId() == productID) {
            orderProductList.remove(i); 
        }
    }

My problem is I did not do this

    session.setAttribute(...)

but when I retrieve session in the jsp, the object stored in the session is changed, how does it works?

hayley
  • 384
  • 5
  • 17
  • What session? This is clearly not just a Java question. Please specify what software stack you are using? – sstan Jul 12 '15 at 03:23
  • 1
    You're modifying a `List` (`orderProductList.remove(i);`) while you're iterating it. You should use an `Iterator` for that. – Titus Jul 12 '15 at 03:32
  • @Titus Why should I use Iterator? I just use for-loop instead. And I do `orderProductList.remove(i);` is because I want to delete it from the list – hayley Jul 12 '15 at 03:48
  • Take a look at this http://stackoverflow.com/questions/223918/iterating-through-a-list-avoiding-concurrentmodificationexception-when-removing – Titus Jul 12 '15 at 04:51
  • 1
    @Titus it actually can't fail in the way that question describes - there is no iterator to get out of sync with the collection. It's still wrong since it ends up skipping list items. – pvg Jul 12 '15 at 05:56

1 Answers1

3

You can think of an HttpSession as a kind of per-user Map that the servlet container maintains for you across requests. Once you initiate a session, the container generates a unique sessionid cookie and sets it in the response and gives you an instance with which you can associate your session data. For subsequent requests, the container takes care of identifying the sessionid cookie and giving you an instance of HttpSession with the data specific to that session. You don't need to call setAttribute just like if you had

SomeObj obj = new SomeObj();
Map<String, SomeObj> map = HashMap();
map.put("key", obj);
SomeObj o2 = map.get("key");
o2.setProperty("foo");

Now you've changed the state of obj while it is still in your map. You do not need to call map.put again.

pvg
  • 2,673
  • 4
  • 17
  • 31
  • Is this because when I set session attribute `session.setAttribute('orderProductList', list)` I named the session "orderProductList" and when I retrieve the session I use a List named "orderProductList" that makes the connection? – hayley Jul 12 '15 at 06:45
  • 1
    @hayley No, it created an attribute on the session called 'orderedProductList', mapped to your list. When you ask for the attribute again, you get the same list. Changing the list does not change the mapping, the session is still holding on to 'orderProductList' -> yourlistInstance. It doesn't matter what modifications you make to the list, the mapping is maintained. – pvg Jul 12 '15 at 06:47
  • Thank you for your help! – hayley Jul 12 '15 at 06:50