File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet --> Jsp with beans" Watch "Servlet --> Jsp with beans" New topic

Servlet --> Jsp with beans

Richard Scothern
Ranch Hand

Joined: May 25, 2001
Posts: 83
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.
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
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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?
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.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63865

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.

[Asking smart questions] [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?
Michael Yuan
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:
Michael Yuan
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Also, you might want to try
instead of the depreciated putValue()
I do not know what difference it makes
but it does not hurt to try.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Servlet --> Jsp with beans
It's not a secret anymore!