4 Replies Latest reply on Jun 12, 2007 6:14 AM by Andrew Rubinger

    how to access more than one EJB containers in one JSP

    shuping chen Newbie

      I need to access EJBs from two different Jboss Servers in one JSP page. Can anybody give me some sample codes?

        • 1. Re: how to access more than one EJB containers in one JSP
          Andrew Rubinger Master

          Haven't checked this in an IDE or anything, but this should get you the idea. Keeping in mind that business functions like obtaining and calling upon EJB3 services shouldn't really be taking place in the view layer...

          <%!
          private YourEjbInterface1 ejb1 = null;
          private YourEjbInterface2 ejb2 = null;
          
          public void jspInit () {
          try {
          
           // Define the Connection Properties
           Properties props1 = new Properties();
           props1.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
           props1.put(InitialContext.PROVIDER_URL, "jnp://yourHost1:yourPort1");
           props1.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
          
           Properties props2 = new Properties();
           props2.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
           props2.put(InitialContext.PROVIDER_URL, "jnp://yourHost2:yourPort2");
           props2.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
          
          
           // Define the Context for Servers
           Context ctx1 = new InitialContext(props1);
           Context ctx2 = new InitialContext(props2);
          
           // Look up the EJB3 Proxies in JNDI
           ejb1 = (YourEjbInterface1) ctx.lookup("ejb1/jndi/name");
           ejb2 = (YourEjbInterface2) ctx.lookup("ejb2/jndi/name");
          
          } catch (Exception e) {
           e.printStackTrace ();
          }
          }
          %>
          
          Invoke 1:
          
          <% ejb1.myMethod(); %>
          
          Invoke 2:
          
          <% ejb2.myMethod(); %>
          
          


          S,
          ALR

          • 2. Re: how to access more than one EJB containers in one JSP
            shuping chen Newbie

            thank you very much.
            I have another question. I want to access two EJB containers in a servlet class. Can I use the functionality of dependecy injection?

            • 3. Re: how to access more than one EJB containers in one JSP
              Andrew Rubinger Master

              You can use Injection in Servlets in the JBoss 5.0.x Beta Series, just not in 4.2 and below; they're not JEE5-conforming implementations.

              S,
              ALR

              • 4. Re: how to access more than one EJB containers in one JSP
                Andrew Rubinger Master

                Sorry, fullr read your questions.

                With injection, you'd be limited to injecting services that are within the same container.

                S,
                ALR