Confused with Session's getAttribute and setAttribute methods
I used the session.set/getAttribute()
to pass my HashMap
to another servlet. On my next servlet, I will add开发者_如何学JAVA a value to my HashMap
but when I search for the value, it can't be read. Why is it not working?
I am setting it as follows:
session.setAttribute("itemList", itemList);
And I am retrieving it as follows:
HashMap itemList = (HashMap)session.getAttribute("itemList");
itemList.put(stockNo, item);
session.setAttribute("itemList", itemList);
Is this correct?
session.setAttribute("itemList", itemList); //is this correct???
In your statement above, you have just associated "itemList" (identifier) to itemList (object). This means, if you want to make a reference later on to this itemList object, you just need to quote the "itemList" identifier.
Java API is your good friend. You should have figured out this pretty easily.
session.setAttribute("itemList", itemList); //IS THIS CORRECT? --- this is the way you are setting it
HashMap itemList = (HashMap)session.getAttribute("hashM"); --- this is the way you are retrieving it
Use the same key "itemList" at both the places.
精彩评论