JavaBean losing reference when passed to JSP - help!
Joined: Jul 20, 2001
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).]
Joined: May 25, 2001
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).]
Joined: Jun 22, 2001
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).]