File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes What's the best way to handle multiple Java bean instance in Jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "What Watch "What New topic

What's the best way to handle multiple Java bean instance in Jsp?

wei Qiao

Joined: Jun 19, 2000
Posts: 23
I am anewbie for Jsp/Servlet and a have a important question:
Does Jsp expose only one java bean instance to the container?
I am using Struts/Jboss/Tomcat solution.
Suppose I have multiple instances to handle in Jsp even different java beans, how do I implement. I am using session or request to pass those beans to a action servlet to do the process. One of my co-worker use client-side Javascript to hold those values.
What's the best way to do it. Is Jsp designed to handle one bean at a time ?

many thanks!
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
There are no restrictions on the number of beans a JSP can use:
<jsp:useBean id="b1" class="com.mycompany.Class1"/>
<jsp:useBean id="b2" class="com.mycompany.Class2"/>
<jsp:useBean id="bn" class="com.mycompany.Classn"/>
Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
wei Qiao

Joined: Jun 19, 2000
Posts: 23
yes, I know that, but my problem is we using the Struts/jboss/tomcat in my project. We use Form beans which handle the Form in JSP and action beans do the dopost/doget kind of thing. It looks like the Struts(ejb container?) will get fields out from Jsp page(which contains a form) and call the appropriate Action bean as well as set the Form bean. In this case, since I only have a field(like textarea) associated with the field of one bean instance(the other instances are stored and active when user select them one by one from a radio button),
How can Struts know which bean instance to be updated?
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Wei,
I am not very sure, if I have understood your requirement.I assume the following:

  1. Strut is the EJBContainer
  2. You have Form Beans and Action Beans defined as Phil has suggested in your JSP

  3. If that is the case, your ActionBean would make an invocation to your EJB Beans in the Strut.The Strut would give the result back to your Action Bean.The instances of the Action Bean would be handled by the JSP Engine, so we need not worry about it.
    Let me know your views on this.

    Sandeep Desai

    1. Sun Certified Java Programmer Scored 93 per cent
    2. Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
    3. IBM Enterprise Connectivity with J2EE Scored 72 per cent
    4. Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
I agree. Here's the link:
subject: What's the best way to handle multiple Java bean instance in Jsp?
It's not a secret anymore!