This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a servlet that needs to set and attribute that will be retrieved by a JSP. Originally my servlet code to do that was: getServletContext().setAttribute("passedItem",passedItemObj);
the JSP had the following code: getServletContext().getAttribute("passedItem"); the code worked except for the fact that all sessions retreived the same values. I then modified the code in the servlet to look like the following: request.setAttribute("passedItem",passedItemObj); and changed the JSP code to look like the following: request.getAttribute("passedItem"); Now I return null values. I tried to change the servlet to use: session.setAttribute("passedItem",passedItemObj); and the JSP to session.getAttribute("passedItem"); and I still return nulls. Any suggestions???
Could you post more code in your servlet? From your description, I believe you are doing right thing, except that you have make sure to create a same session: getSession(true), and use Dispatch to forward to jsp page. Thanks
Joined: Jul 01, 2003
I am using HttpSession session = request.getSession(true); in the servlet as well as using the dispatch.forward(request, response); to forward to the JSP. As stated previously, it was working when I used getServletContext() with the setAttribute and in the JSP's getAttribute, but the problem was that all the JSP's returned the same values. I wanted to limit it to either the session or the request. The scenario is: user calls servlet1 via the web browser, if I am unable to retrieve thier data then I set attributes in a custom error.jsp file and forward to user (worked when using getServletContext() to set attributes). If I retrieve data, I send an html file that requests more information and performs a post to a second servlet. In the second servlet, I perform a request.setAttribute("passed",passedObj) and a dispatch.forward(request, response) to a customInfo.jsp file. The customInfo.jsp portion works fine, its the first situation where the first servlet is called that I fail.