4 Replies Latest reply on Apr 2, 2002 12:12 AM by david_cohoon

    Calling EJBs inside servlet

    david_cohoon

      During a test, I successfully created instances of stateless session beans from an application. I'm trying to repeat this inside my web application. The web design is close to the struts model. My servlet uses helper classes ("Action Classes") to process requests. The same accessing code has been used here as in my application. At the point that the context tries the "lookup" method, I receive an exception without any message.

      Is there something special needed inside a web application to call ejbs?

        • 1. Re: Calling EJBs inside servlet
          drcharris

          Post details of the exception and perhaps others on the forums can help you. Using EJBs from a servlet should, for the servlet code itself, be no different from using a plain java client application.

          Without any context to work from, I'd say the most likely problem is that your InitialContext isn't being created properly since it can't find the 3 properties it needs (those in jndi.properties). You need to make sure this file is on your classpath.

          If that's not it, post details of the exception you get (a full stack trace and the few lines of code preceding the exception) and we'll track it down further.

          • 2. Re: Calling EJBs inside servlet
            david_cohoon

            Here is the source for calling my session ejb:

            try {
            Properties jndiProperties = new Properties();
            jndiProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            jndiProperties.setProperty(Context.PROVIDER_URL, "localhost:1099");
            jndiProperties.setProperty(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
            Context initialContext = new InitialContext(jndiProperties);
            Object ref = initialContext.lookup("UserSession");
            UserSessionHome userHome = (UserSessionHome)javax.rmi.PortableRemoteObject.narrow(ref,UserSessionHome.class);
            UserSessionRemote userRemote = userHome.create();
            strTest = userRemote.test();
            userRemote.remove();
            }
            catch (Exception e) {
            throw new ServletException(e);
            }

            I took this source and created a test servlet. The stack reports a ClassCastException for the narrow method. There was nothing like this when executing this source from a helper class.

            I use a utility to build my ejbs for JBoss, it automatically builds them into jars for me. In my test application I referenced a client jar with sun's deploytool. I tried referencing this inside my web application, but Forte reported errors finding the class. So I copied the original java files from the ejb for reference so I could compile my web application. Could this be where I've gone wrong?

            this is the stack trace reported from IPlanet 6.0 webserver:
            Internal error: servlet service function had thrown ServletException (uri=/NuclearWeb/servlet/actions/testservlet): javax.servlet.ServletException, stack: javax.servlet.ServletException at actions.testservlet.service(testservlet.java:60) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:897) at com.iplanet.server.http.servlet.WebApplication.service(WebApplication.java:1065) at com.iplanet.server.http.servlet.NSServletRunner.ServiceWebApp(NSServletRunner.java:959) , root cause: java.lang.ClassCastException at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:296) at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137) at actions.testservlet.service(testservlet.java:54) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.iplanet.server.http.servlet.NSServletRunner.invokeServletService(NSServletRunner.java:897) at com.iplanet.server.http.servlet.WebApplication.service(WebApplication.java:1065) at com.iplanet.server.http.servlet.NSServletRunner.ServiceWebApp(NSServletRunner.java:959)

            • 3. Re: Calling EJBs inside servlet
              drcharris

              OK your JNDI code looks fine. It's quite possible to get ClassCastExceptions when you've made changes to the Home interface on JBoss but are still using an older version of that class (UserSessionHome.class) on the web server.

              Check which jars are providing instances of UserSessionHome.class to both JBoss and the web server. If you're using JBoss 2.4.x then it will first look in its system classpath (anything under %JBOSS_HOME%/lib, lib/ext) to find the class and then in your application jar/ear (presumably under /deploy). If you make a client jar as part of your build process make sure it's up to date w.r.t. your application. If you hot-deploy to JBoss, make sure your webserver cycles as well.

              If you're *totally* sure the versions of the class are the same then you've got a more serious problem.

              • 4. Re: Calling EJBs inside servlet
                david_cohoon

                I've resolved my problem,
                Even though the test application worked fine, IPlanet web server could not resolve the name "UserSessionHome".

                I recreated the bean within a package, "testing.UserSessionHome",
                and I get my home interface without any difficulties.

                I guess I get what I deserve for not being very organized.

                Thanks for you insight.