dcsimg
JSP-Bean-Servlet-Bean-JSP
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Wayne_Hunter
Posted On:   Thursday, May 3, 2001 07:44 AM

IDE: JBuilder4 JSP/Servlet: Tomcat 3.1 Does anyone know how to access a Bean from a servlet?. The Bean properties are set using introspection by a JSP page. I'd like to then perform processing within a servlet using the properties set within the Bean. Then finally use the Bean with another JSP. I've tried various methods documented at various sites with no success. It seems I cannot obtain a reference to the Bean without losing the property values. Any suggestions? Below is a snippet from the jGuru JSP Short course outlining what I'd like to do. It simply doesn't work in my environment. I've played around with the contexts with no success. Consider the following JSP p   More>>

IDE: JBuilder4


JSP/Servlet: Tomcat 3.1



Does anyone know how to access a Bean from a servlet?.

The Bean properties are set using introspection by a JSP page.

I'd like to then perform processing within a servlet using the properties set within the Bean.

Then finally use the Bean with another JSP.


I've tried various methods documented at various sites with no success.

It seems I cannot obtain a reference to the Bean without losing the property values. Any suggestions?



Below is a snippet from the jGuru JSP Short course outlining what I'd like to do. It simply doesn't work in my environment. I've played around with the contexts with no success.






Consider the following JSP page, say Bean1.jsp, which creates a named instance fBean of type FormBean, places it in the request, and forwards the call to the servlet JSP2Servlet.


Observe the way the bean is instantiated--here we automatically call the bean's setter methods for properties which match the names of the posted form elements, while passing the corresponding values to the methods.





scope="request"/>





The servlet JSP2Servlet now extracts the bean passed to it from the request, makes changes using the appropriate setters, and forwards the call to another JSP page Bean2.jsp using a request dispatcher. Note that this servlet, acting as a controller, can also place additional beans if necessary, within the request.



public void doPost (HttpServletRequest request,
HttpServletResponse response) {

try {
FormBean f = (FormBean) request.getAttribute
("fBean");

f.setName("Mogambo");

// do whatever else necessary

getServletConfig().getServletContext().
getRequestDispatcher("/jsp/Bean2.jsp").
forward(request, response);

} catch (Exception ex) {

. . .

}

}



The JSP page Bean2.jsp can now extract the bean fBean (and whatever other beans that may have been passed by the controller servlet) from the request and extract its properties.









scope="request"/>






   <<Less

Re: JSP-Bean-Servlet-Bean-JSP

Posted By:   AlessandroA_Garbagnati  
Posted On:   Thursday, May 3, 2001 10:26 AM

Hi,

Try changing this line:


getServletConfig().getServletContext(). getRequestDispatcher("/jsp/Bean2.jsp"). forward(request, response);


with this one:


request.getServletContext(). getRequestDispatcher("/jsp/Bean2.jsp"). forward(request, response);

Re: JSP-Bean-Servlet-Bean-JSP

Posted By:   John_Zukowski  
Posted On:   Thursday, May 3, 2001 10:16 AM

getAttribute() is a 2.1 API. If you look in the Servlets and/or JSP FAQ, you'll see how to do what you are after with the appropriate 2.2 API.
About | Sitemap | Contact