• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

POSTing from servlet to JSP

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi could someone help me out with a simple and (presumably quick) fix?
I've recently started getting into Servlets/JSP and was wondering, I have a servlet which redirect to a JSP. I would like the JSP to display data stored in a Bean.
I know how to create an instance of the bean in the servlet and set the bean attributes to the correct values (taken from another JSP), but when I redirect to the JSP page all the values ra eset to NULL. I think its something to do with the pageContext, I think the Bean data is lost during the redirect.
PS: A little exmaple code would be nice, I can find nothing on the net.
Thanks
KPS
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You could put your values in the session and then forward it to jsp.
In you servlet you could set the values as :
session.setAttribute("DBNAME",dbname);
and get those value in your jsp as below.
String name=session.getAttribute("DBNAME");
-Amol
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than redirecting to the JSP, you can use a RequestDispatcher to forward from the servlet to the JSP. This all takes place within the same request context so that if you place the bean on the request, it will still be there when the JSP gets executed. In contrast, a redirect causes a new request to be initiated.
hth,
bear
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the Bean data is lost during the redirect.


It is not the bean data that is being lost across the redirect, but the bean itself. If you check, you will find that because a new request is initiated by the redirect that a new instance of the bean is being created for your page.
hth,
bear
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you might decide to have the bean in session scope and you can use either forward or include. In this case you need to make sure you don't create another instance of the bean in the jsp page - specify the bean type and not the class.
[ June 19, 2003: Message edited by: Calina Cazangiu ]
 
Bob Backlund
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thanks for the replies but I'm still trying. However I'm getting fedup now and just want this thing to work, I've spent far too long trying to figure it out now and I fedup with it, so here goes. Thgis is my servlet and my JSP can some please just 'fill in the blanks' if you like and show me where its going wrong (probably more than one place by now), cause Im close to just giving up.
detailsServlet
-----------------------------------------------

-----------------------------------------------
outputdetails.jsp
-----------------------------------------------

-----------------------------------------------

Sorry if this seems a little OTT but I can't believe how hard it is to find an answer to this on the net.
Thankx
KPS
[ June 19, 2003: Message edited by: Cindy Glass ]
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet and JSP CODE LOOKS fine to me.
Are you getting any errors?
 
Bob Backlund
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No erros unless you consider all the values coming back as NULL as an error.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey!,kps.
I perhaps encounter the same question,I think if you should use session.getAttribute("detail")to get detailBean instance. then castType it.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry kps,by the way,Bear


This all takes place within the same request context so that if you place the bean on the request


How can I put the bean on the request?
 
Pradeep bhatt
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the doPost method getting called?
Replace session.setattribute by request.setAttribute to put a bean into session.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can I put the bean on the request?



hth,
bear
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,Bear! thank you!
I will try to do it.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,kps,I got it.
you can use following code.

then you can get what you hope so.
ok,hope your helps.
 
Bob Backlund
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey cool it worked.
Now for my next trick: JSP to Servlet, Servlet to Bean, Bean to DB, and back again!
I'm sure I'll be back soon.
KPS
reply
    Bookmark Topic Watch Topic
  • New Topic