aspose file tools*
The moose likes Servlets and the fly likes POSTing from servlet to JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "POSTing from servlet to JSP" Watch "POSTing from servlet to JSP" New topic
Author

POSTing from servlet to JSP

Bob Backlund
Ranch Hand

Joined: Jun 05, 2003
Posts: 51
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


Cheers!
Amol Desai
Ranch Hand

Joined: Jan 29, 2003
Posts: 82
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

Joined: Jan 10, 2002
Posts: 60994
    
  65

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60994
    
  65

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

Joined: Feb 27, 2003
Posts: 30
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

Joined: Jun 05, 2003
Posts: 51
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

Joined: Feb 27, 2002
Posts: 8919

The servlet and JSP CODE LOOKS fine to me.
Are you getting any errors?


Groovy
Bob Backlund
Ranch Hand

Joined: Jun 05, 2003
Posts: 51
No erros unless you consider all the values coming back as NULL as an error.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
hey!,kps.
I perhaps encounter the same question,I think if you should use session.getAttribute("detail")to get detailBean instance. then castType it.


Thanks...qingwu<br />When I open my eyes,I see your pretty face.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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

Joined: Feb 27, 2002
Posts: 8919

Is the doPost method getting called?
Replace session.setattribute by request.setAttribute to put a bean into session.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60994
    
  65

How can I put the bean on the request?


hth,
bear
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
Hey,Bear! thank you!
I will try to do it.
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
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

Joined: Jun 05, 2003
Posts: 51
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
 
subject: POSTing from servlet to JSP