• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet --> Jsp with beans

 
Richard Scothern
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richard Scothern
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your "null object" error message
come from the "useBean" tag?
[ March 10, 2002: Message edited by: Michael Yuan ]
 
Michael Yuan
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic