• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Servlet to JSP communication

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic