我使用session.set/getAttribute()
將我的HashMap
傳遞給另一個servlet。在我的下一個servlet中,我將爲我的HashMap
添加一個值,但是當我搜索該值時,無法讀取它。爲什麼它不起作用?與Session的getAttribute和setAttribute方法混淆
我如下設置它:
session.setAttribute("itemList", itemList);
而且我如下取回:
HashMap itemList = (HashMap)session.getAttribute("itemList");
itemList.put(stockNo, item);
session.setAttribute("itemList", itemList);
這是正確的嗎?
只需檢查AddtheEntry servlet是否提供新會話或舊會話。 – Ankit 2011-03-02 05:47:39
@我該如何檢查?謝謝 – newbie 2011-03-02 05:49:39
請注意,第二個servlet中的setAttribute調用是不必要的。無論如何,所做的更改都會反映在所引用的對象中。這是Java,而不是PHP或一些非OO語言。 – BalusC 2011-03-02 12:49:24