aspose file tools*
The moose likes Servlets and the fly likes Why does my JSP looses JavaBean values! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why does my JSP looses JavaBean values!" Watch "Why does my JSP looses JavaBean values!" New topic
Author

Why does my JSP looses JavaBean values!

Bob Backlund
Ranch Hand

Joined: Jun 05, 2003
Posts: 51
I have a simple JSP/Servlet combo that allows a user to enter details into text fields in a JSP, on submitting a POST the Servlet creates an instance of a JavBean to store the details then redirects to another JSP which displays the details (with me so far?)
Problem is when you get to the second JSP the values are set to 'null', now if you insert a System.out in the servlet to display the values stored in the JavaBean, you get the correct data (taken from the 1st JSP) but the 2nd JSP gets a null value.
Basically somewhere between the servlet populating the fields in the JavaBean and redirecting to the 2nd JSP the values seem to be lost.
I have the <jsp:useBean id="details" class="javasrc.beans.detailsBean" scope="page" />
in my 2nd JSP and the Servlet is definately populating the values in the JavaBean, so I can't for the life of me see whay the 2nd JSP is displying 'null' (<%= details.getFname() %>
Please can anyone tell me what I'm leaving out?
(I got a feeling it's gonna turn out to be something stupidly obvious).


Cheers!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
I suspect the scope="page" is forcing the creation of a new JavaBean and the JSP never looks for a bean in the session. Try scope="session"
Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Or, do a forward instead of a redirect and place the bean in request scope.
bear


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

Joined: Jan 10, 2002
Posts: 61606
    
  67

Expounding:
Firstly, after you create your bean in the servlet, how are you attaching it to the page context? There's no pageContext in the servlet.
What is most common, is to create the bean in the servlet, attach it to the request (using request.setAttribute()), forward to the page, and retrieve the bean from request context on the page.
Note 1: if no bean is found in the specified context, the useBean tag creates a fresh instance (this is why you are seeing all those nulls).
Note 2: you must forward rather than redirect, since a redirect causes the current request to end and a new one to be created.
Note 3: if you must redirect (rather than forward), you need to store the bean in either session or application scope for it to "survive" the transition.
hth,
bear
Bob Backlund
Ranch Hand

Joined: Jun 05, 2003
Posts: 51
Wow cool. Three replies in less than a day, and all look useful.
Many thanks guys.
KPS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does my JSP looses JavaBean values!