File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet to JSP communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet to JSP communication" Watch "Servlet to JSP communication" New topic
Author

Servlet to JSP communication

Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
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???
j2eelearner
Greenhorn

Joined: Dec 02, 2003
Posts: 2
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
Randall Stevens
Ranch Hand

Joined: Jul 01, 2003
Posts: 65
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet to JSP communication