4 Replies Latest reply on Feb 6, 2002 2:19 AM by sammy-t

    jboss and jbuilder, ejb & servlets - how?

    adrian79

      Hello everybody,

      I'm new to both EJB's and JBoss. I'm trying to integrate Borland's JBuilder to work with JBoss. I can run a test program from jbuilder that accesses a session ejb from jboss and calls some of its methods.

      The problem is when I try to access the session ejb from a servlet. I know I have to declare the ejb's I access ( and I did that, although I'm not sure if I did it right ). Also, to test my servlet I use the built-in tomcat that comes with JBuilder.

      In my standalone program (which works ok) I have something like:

      InitialContext jndiContext = new InitialContext();
      TestHome home = (TestHome) jndiContext.lookup("test/Test");
      Test test = home.create();
      test.doSomething();

      Using the exact same code in a servlet I get the following exception:

      javax.naming.CommunicationException [Root exception is java.lang.ClassNotFoundException: com.adrian.ejbtest.TestHome]

      So my question is: does anyone have a working example for a servlet running with jbuilder's built-in tomcat, that successfully accesses a session ejb and calls one of its methods? Does anyone have a guide on how to do this? Or do I have to use the tomcat that comes with jboss, and deploy a web application etc.

      Any ideas are highly appreciated.

      Thank you,
      Adrian.

        • 1. Re: jboss and jbuilder, ejb & servlets - how?
          seybaaa

          well I have made a war with my servlet,and a jar
          with my ejb,then just copy the war in tomcat webApp
          then just pus jar in jboss deploy that works very find
          good luck

          • 2. Re: jboss and jbuilder, ejb & servlets - how?
            sebaseba

            I do not know the details of your JNDI configuration, but it looks to me like you need to replace

            InitialContext jndiContext = new InitialContext();

            by something like this:

            Properties prop = new Properties();
            prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            prop.put(Context.PROVIDER_URL, "localhost:1099");
            Context ctx = new InitialContext(prop);

            Otherwise your servlet does not know how to connect to JNDI, as suggested by the error message you are getting.

            Sebastian

            • 3. Re: jboss and jbuilder, ejb & servlets - how?
              alcaponi

              Hi all,
              Following Sebastian's post, I'm trying to run a servlet on Tomcat 3.2 that would look up an EJB on JBoss 3.0a and invoke that EJB's method.
              (Both Tomcat and JBoss are on my localhost)
              Now when I invoke the servlet from a JSP, I get the following error:

              Internal Servlet Error:

              javax.servlet.ServletException: Naming Exception creating Initial Context
              Cannot instantiate class: org.jnp.interfaces.NamingContextFactory
              at com.mybean.CalculateLoanServlet.init(CalculateLoanServlet.java:34)
              at org.apache.tomcat.core.ServletWrapper.doInit(ServletWrapper.java:317)
              at org.apache.tomcat.core.Handler.init(Handler.java:215)
              at org.apache.tomcat.core.ServletWrapper.init(ServletWrapper.java:296)
              at org.apache.tomcat.core.Handler.service(Handler.java:254)
              at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
              at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:806)
              at org.apache.tomcat.core.ContextManager.service(ContextManager.java:752)
              at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
              at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
              at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
              at java.lang.Thread.run(Thread.java:484)


              This is the servlet init method where the exception occurs.

              import java.io.*;
              import java.util.*;
              import javax.rmi.PortableRemoteObject;
              import javax.servlet.*;
              import javax.servlet.http.*;
              import javax.naming.InitialContext;
              import javax.naming.Context;
              import javax.naming.NamingException;
              import org.jnp.interfaces.NamingContextFactory;

              public class CalculateLoanServlet extends HttpServlet
              {
                private static final String CONTENT_TYPE = "text/html";
                protected Context initCtx = null;

                /**Initialize global variables*/
                public void init(ServletConfig config) throws ServletException
                {
                  super.init(config);
                  try
                  {
                    Hashtable env = new Hashtable();
                    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                    env.put(Context.PROVIDER_URL, "localhost:1099");
                    Context initCtx = new InitialContext(env);
                  }
                  catch(NamingException ne)
                  {
                    throw new ServletException("Naming Exception creating Initial Context " +
              ne.getMessage());
                  }
                }
              ...
              }


              Can anyone help?
              Thanx!
              Al

              • 4. Re: jboss and jbuilder, ejb & servlets - how?
                sammy-t

                The original error seemed to be a classpath problem. You can test that theory quickly by copying $JBOSS_HOME/client/*.jar to $JAVA_HOME/jre/lib/ext then restart your server. This will only help on the ClassNotFound errors, the other errors appear to be genuine JNDI configuration problems that I haven't seen yet :) If that 5 second hack fixes your ClassNotFound error, then you need to fix your classpath properly else the Java-Grue will eat you. I run Tomcat independantly of JBoss, and I had that problem because Tomcat's startup script wasn't adding jboss/client to the extdirs.