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

Why does my JSP looses JavaBean values!

 
Bob Backlund
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13047
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, do a forward instead of a redirect and place the bean in request scope.
bear
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64188
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic