newbie
newbie

Reputation: 14950

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 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?

Upvotes: 1

Views: 6604

Answers (2)

Sudhanshu Umalkar
Sudhanshu Umalkar

Reputation: 4202

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.

Upvotes: 1

logitab
logitab

Reputation: 26

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.

Upvotes: 1

Related Questions