session.setAttribute returns null
800614Feb 23 2008 — edited Feb 26 2008Hi
My problem is like this:
I have two servlets: s1 and s2. I want to pass a value from s1 to s2. I do this using HttpSession. But I am not able to retrieve the value in the second servlet. The code in S1's doPost():
{
HttpSession session = req.getSession();
session.setAttribute("TestAttribute", "TestAttribute");
RequestDispatcher dispatch = req
.getRequestDispatcher("/s2");
dispatch.forward(req, res);
}
The code in S2's doGet():
{
HttpSession session = req.getSession();
String value = (String) session.getAttribute("TestAttribute");
PrintWriter w = req.getWriter();
w.write(value);
}
I don't get any values in the second page. I also tried using HttpRequest.getAttribute() and setAttribute. Nothing is working. In the application I actually wanted to pass an object of a user defined class, instead of the string value I passed.
Please help me to find out what is wrong with this.
Thanks
Geet