File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark ""jsp:usebean" question" Watch ""jsp:usebean" question" New topic

"jsp:usebean" question

Don Martino

Joined: Oct 04, 2008
Posts: 25
I define a bean in first.jsp using "jsp:usebean" with "scope=session" and populate all fields in bean. I then forward to second.jsp which makes no reference to bean created in first.jsp. Then second.jsp forwards to third.jsp, which has "jsp:usebean" defined for the bean created in first.jsp.

I lost all of the values which were populated from first.jsp. I thought that as long as I was in same session the bean will retain all values regardless of where I go.

<jsp:useBean id="mauMainForm" class="com.att.vtone.mau.MauMainForm" scope="session" >
<jsp:setProperty name="mauMainForm" property="*"/>

second.jsp: none

<jsp:useBean id="mauMainForm" class="com.att.vtone.mau.MauMainForm" scope="session" >


Question: Do I need to define the bean in every jsp in order to retain the values in the bean?
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

No you don't need to define it in every page. As long as your session is maintained then the code you have should work. Things to check:
1) Spelling
2) Spelling
3) Spelling ;-)
4) Is your session being maintained?

To test, you can do:

You should get true just once for the first output line and false the rest of the time, and the same id number for all requests.

If you don't then you have to ask yourself why aren't my sessions being maintained. The most probably reason would be that your client doesn't have cookies enabled. By default, sessions are maintained by cookies so if your client has cookies turned off, then the session will be lost every response cycle. You should use URL Re-writing to prevent that. But there are other reasons which only your environment could tell you.

If your session is being maintained but you still don't get to keep the object then you need to test a few other things. Is the object successfully being created? Check your log files for errors. Is it being removed? Look around your code base to see if there is some line of code that sets the same value to null or empties session attributes.

Another thing to double check is what do you actually mean by 'not retaining values'. Does the bean get re-created in page 3 or do the values that get stored in page 1 get lost? Check this by first making sure that the values you set on page 1 are actually set the way you think they should be (ie print them out). Then on page 3 change the jsp:useBean tag to:

This will force the JSP to use a previously stored mauMainForm, not create a new one. If the one you created in page 1 still exists, it will be used, if not you get an error. The error will make it clear that the object you created in page 1 is lost, so you can narrow your search to how that happens. If no error occurs then you know the values were not stored properly or were later erased, so you can, again, narrow your search for what it wrong.

Don Martino

Joined: Oct 04, 2008
Posts: 25
Thanks Steve!


You were correct, spelling was wrong. Made correction and it works!!
I agree. Here's the link:
subject: "jsp:usebean" question
jQuery in Action, 3rd edition