wood burning stoves 2.0*
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
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
Author

JavaBean losing reference when passed to JSP - help!

Steven Briggs
Greenhorn

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 =
getServletContext().getRequestDispatcher("/ContactForm.jsp");
IndividualOperation conOp = new IndividualOperation();
session.putValue("op", conOp);
dispatcher.forward(request,response);
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!
Thanks
Steven

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

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 =
getServletContext().getRequestDispatcher("/ContactForm.jsp");
use :
RequestDispatcher dispatcher =
request.getRequestDispatcher("/ContactForm.jsp");
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:
session.getAttribute("op");
hope this helps.

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


Subbu
 
jQuery in Action, 2nd edition
 
subject: JavaBean losing reference when passed to JSP - help!
 
Similar Threads
removeAttribute() question
Jsp and javabean
Javabean reference
Not including the template when usin RequestDispatcher
Calling an EJB from within a Servlet - problem with lookup()