Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

session.setAttribute returns null

800614Feb 23 2008 — edited Feb 26 2008
Hi

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 25 2008
Added on Feb 23 2008
14 comments
1,202 views