10 Replies Latest reply on Mar 19, 2003 5:04 PM by boooj um

    calling ejb from servlet/jsp

    ku916 Newbie

      I've installed jboss 3.0.1 with tomcat 4.0.4. I would like to know how to call an ejb (already deployed) from tomcat.

      I assume I'll need to update tomcat's server.xml, but not sure how? or if there is any examples out there online?

      thank you
      ku

        • 1. Re: calling ejb from servlet/jsp
          Joel Vogt Master

          you'll have to do something like this to get a reference to the home interface of your ejb

          Object object = initialContext.lookup(YOUR_EJB_JNDI_NAME);
          YourEJBHome home = (YourEJBHome)
          PortableRemoteObject.narrow(object, YourEJBHome.class);

          Then do home.findAll() etc...

          Where you do this lookup is up to you. I would recommend using a framework such as struts, from jakarta.apache.org

          • 3. Re: calling ejb from servlet/jsp
            ku916 Newbie

            Thanks for the info. I've found an exmaple online to tryout a first EJB with JSP client. The EJB referencing in the JSP code is similar to your comments

            long t1 = System.currentTimeMillis();
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
            props.put(Context.PROVIDER_URL, "http://w2p-clo:1099");

            Context ctx = new InitialContext(props);
            FirstHome home = (FirstHome)ctx.lookup("ejb/First");
            First bean = home.create();
            String time = bean.getTime();
            bean.remove();
            ctx.close();

            However, I went into a different problem when trying to access the JSP.

            javax.servlet.ServletException: Receive timed out
            javax.naming.CommunicationException: Receive timed out. Root exception is java.net.SocketTimeoutException: Receive timed out


            I'm running JBoss 3.0.1 with Tomcat 4.0.4 bundle and JDK 1.4

            • 4. Re: calling ejb from servlet/jsp
              ku916 Newbie

              now I run into a new problem

              javax.servlet.ServletException: Receive timed out

              javax.naming.CommunicationException: Receive timed out. Root exception is java.net.SocketTimeoutException: Receive timed out


              here is my JSP code (example found online)
              <%@ page import="javax.naming.InitialContext,
              javax.naming.Context,
              java.util.Properties,
              com.stardeveloper.ejb.session.First,
              com.stardeveloper.ejb.session.FirstHome"%>
              <%
              long t1 = System.currentTimeMillis();
              Properties props = new Properties();
              props.put(Context.INITIAL_CONTEXT_FACTORY,
              "org.jnp.interfaces.NamingContextFactory");
              props.put(Context.PROVIDER_URL, "http://w2p-clo:1099");

              Context ctx = new InitialContext(props);
              FirstHome home = (FirstHome)ctx.lookup("ejb/First");
              First bean = home.create();
              String time = bean.getTime();
              bean.remove();
              ctx.close();
              long t2 = System.currentTimeMillis();
              %>

              • 5. Re: calling ejb from servlet/jsp
                ku916 Newbie

                OK! I made a mistake on the Context.PROVIDER_URL "http://w2p-clo:1099" Now I've changed it to "jnp://w2p-clo:1099"

                but I get this error instead

                java.rmi.MarshalException: error marshalling arguments; nested exception is:
                java.net.SocketException: Software caused connection abort: socket write error

                • 6. Re: calling ejb from servlet/jsp
                  Blue King Newbie

                  I am having the exact same problem. The ejb can be invoked from a standalone client (diff jvm) without problem. But it called from a servlet in within tomcat, it fails.

                  Any help is appreciated.

                  • 7. Re: calling ejb from servlet/jsp
                    pol.leleux Newbie

                    I have exactly the same problem running Tomcat 4.0.4 as Standalone when I try to instanciate a bean within JBoss 3.0.4. (I also use JAAS).
                    Has anybody found a solution ? Any help would be greatly, greatly appreciated !

                    • 8. Re: calling ejb from servlet/jsp
                      Joerg Thoennes Newbie

                      > OK! I made a mistake on the Context.PROVIDER_URL
                      > "http://w2p-clo:1099" Now I've changed it to
                      > "jnp://w2p-clo:1099"

                      If your Tomcat is running in the same JVM, i.e. deployed as
                      a container inside JBoss, it is sufficient to use

                      Context = new InitialContext();

                      without any further arguments. In this way, you get a
                      reference to the current JBoss JNDI service, which also
                      has all EJBs registered.

                      Your approach differs in the way that you try to access
                      the JBoss JNDI as an external service from inside JBoss.
                      Since JBoss has some optimized stacks for Intra-JVM calls,
                      this does not match.

                      Perhaps my technical explanation above it not quite
                      correct (some developer please comment), but it works
                      for us as described above.

                      Cheers, Jörg


                      • 9. Re: calling ejb from servlet/jsp
                        seemanto Newbie

                        hi

                        i have my tomcat and jboss running in seperate jvm's.I do get the home object in the servlet but when i call create() on it, there's an exception.I really don't have any clue what going wrong.

                        • 10. Re: calling ejb from servlet/jsp
                          boooj um Newbie

                          the manual referred to was for ejb to ejb not servlet to ejb...please help