wood burning stoves 2.0*
The moose likes JSP and the fly likes Servlet --> Jsp with beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet --> Jsp with beans" Watch "Servlet --> Jsp with beans" New topic
Author

Servlet --> Jsp with beans

Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Hi,
I'm setting the properties of a bean from some get request parameters in a servlet, and using a request dispatcher to forward the request to a jsp page.
I'm using the bean in the jsp page, but when I try and retreive the values from the bean, they are null.
I've set the bean as a session bean, is there something I'm missing.
Richard
greg philpott
Ranch Hand

Joined: Nov 10, 2000
Posts: 73
yo richy!!
i'll drop you an e-mail to catch up.
What I think is your problem is the scope of your javabean, make it scope="request" in your to and from jsp's.
Alternatively, keep it session, and implicily set the javabeans properties in the destination jsp page from the request(to do this your input field names have to match up with your javabean variables) -and you'll have the bonus of not having to set them in your servlet
later,
Greg
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
It sounds like your JSP is building a new bean instead of using the one attached to the session. Are you sure you are using the same name in both JSP and servlet?
Bill
Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
Hey guys,
Thanks for your replies. Greg, I didn't know you where still spreading your knowledge, good to hear from you.
I don't think i explained myself so here goes...
In my servlet, I am creating an instance of a bean, populating the variables from a get request and then I use:
HttpSession session = request.getSession(true);
session.putValue("transformer", transformerBean);
In the jsp page, I have the following:
<jsp:useBean id="transformer" class="TransformerBean" scope="application" />
Except now I get the error:
Attempted a bean operation on a null object
So I assume I'm missing something somewhere, probably in the servlet.
Richard
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

You are storing the bean in session scope, yet looking for it in application scope!
Change your useBean to specify session scope and you should hook up to the same instance of the bean.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
I tried putting the bean in session scope but the problem persists. Could there be an error within the bean class?
Richard
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Does your "null object" error message
come from the "useBean" tag?
[ March 10, 2002: Message edited by: Michael Yuan ]

Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Also, you might want to try
session.setAttribute()
instead of the depreciated putValue()
I do not know what difference it makes
but it does not hurt to try.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet --> Jsp with beans