This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

POSTing from servlet to JSP

 
Bob Backlund
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Amol Desai
Ranch Hand
Posts: 82
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Calina Cazangiu
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No erros unless you consider all the values coming back as NULL as an error.
 
qingwu wang
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I put the bean on the request?


hth,
bear
 
qingwu wang
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,Bear! thank you!
I will try to do it.
 
qingwu wang
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic