It's not a secret anymore!
The moose likes Servlets and the fly likes JavaBean losing reference when passed to JSP - help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JavaBean losing reference when passed to JSP - help!" Watch "JavaBean losing reference when passed to JSP - help!" New topic

JavaBean losing reference when passed to JSP - help!

Steven Briggs

Joined: Jul 20, 2001
Posts: 6
I'm writing a web application using Servlets, JavaBeans and JSPs. I have a
problem when using a Servlet to create a JavaBean and then place the JavaBean into the
session object (using HttpSession.putValue), and then using
RequestDispatcher to forward control to a JSP. The JSP then uses "useBean"
to get a reference to the bean I just placed in the session. Fine. The only
problem is that somewhere, somehow when the JSP uses the bean other objects
which that bean has internal references to are lost, i.e. the variables
point to null. If I do everything from within the servlet, its fine.
Presumably the reference is ceasing to point to the object - but why.
I'm running JRun 3 and IIS.
The servlet code looks like this:
RequestDispatcher dispatcher =
IndividualOperation conOp = new IndividualOperation();
session.putValue("op", conOp);
The JSP code like this:
<jsp:useBean id="op" class="com.occam.contacts.IndividualOperation" />
<%= op.generateHTML()%>
And the bean code like this:
String strHTML = this.con.getAddressLine1();
return strHTML;
where con is a private variable within the bean which is a reference to
another object. I hope the code is of use I wasn't sure whether to include
it or not.
Please help, I'm absolutely desperate!

[This message has been edited by Steven Briggs (edited July 20, 2001).]
nagesh vutukuru

Joined: May 25, 2001
Posts: 10
U have to use scope attribute in usebean.
<jsp:useBean id="op" class="com.occam.contacts.IndividualOperation" scope="session"/>
The bean should contain all the data before u put into session.
[This message has been edited by nagesh vutukuru (edited July 20, 2001).]
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi Steve,
try these changes to your code
instead of
RequestDispatcher dispatcher =
use :
RequestDispatcher dispatcher =
instead of :
session.putValue("op", conOp);
try :
session.setAttribute("op", conOp);
session.putValue() has been deprecated due to security reasons.u can retrieve the object using:
hope this helps.

[This message has been edited by Subbu Aswathanarayan (edited July 20, 2001).]

I agree. Here's the link:
subject: JavaBean losing reference when passed to JSP - help!
It's not a secret anymore!