9 Replies Latest reply on Apr 17, 2006 4:20 AM by José Luis Laviña

    Accessing an EJB from a Servlet

    José Luis Laviña Newbie

      Hello,

      I'm trying to access to an EJB (3.0, defined with annotations) from a Servlet, but the InitialContext doesn't contain the appropiate reference and I obtain a javax.naming.NameNotFoundException.

      Is necessary to define the EJB also into web.xml or jboss-web.xml?
      Is there an example of this anywhere?

      Thanks!!
      José Luis.

        • 1. Re: Accessing an EJB from a Servlet
          Alexander Sack Novice

           

          "jllavina" wrote:
          Hello,

          I'm trying to access to an EJB (3.0, defined with annotations) from a Servlet, but the InitialContext doesn't contain the appropiate reference and I obtain a javax.naming.NameNotFoundException.

          Is necessary to define the EJB also into web.xml or jboss-web.xml?
          Is there an example of this anywhere?

          Thanks!!
          José Luis.


          No. Can you please post your JNDI view from the JMX-Console (localhost:8080)->JNDI service->list()?

          More than likely your lookup is wrong or your EJB3 is not bound properly?

          Just out of shear curiousity, where are your EJB3's packaged? You can't put them in your WAR as classes because the EJB3 deployer needs to see them. You should have an EAR with a JAR and a WAR module defined or separate WAR and JAR files deployed (JAR being deployed first).

          • 2. Re: Accessing an EJB from a Servlet
            José Luis Laviña Newbie

            My application is packaged using an EAR (ysf.ear), with a JAR (ysf.jar) and a WAR (ysf.war), and the JAR being deployed first.

            The jmx-console doesn't show me the JNDI service, the only place where I found references to my application is here:

            jboss.j2ee

            * ear=ysf.ear,jar=ysf.jar,name=Employee,service=EJB3
            * module=ysf.jar,service=EJB3
            * service=ClientDeployer
            * service=EARDeployer
            * service=EARDeployment,url='ysf.ear'

            This is the log shown in the console window when the application is deployed:

            08:55:07,441 INFO [EARDeployer] Init J2EE application: file:/J:/jboss-4.0.3SP1/server/all/deploy/ysf.ear
            08:55:07,882 INFO [Ejb3AnnotationHandler] found EJB3: ejbName=Employee, class=es.dbs.ysf.employee.EmployeeSessionBean, type=STATELESS
            08:55:07,892 INFO [Ejb3Deployment] EJB3 deployment time took: 90
            08:55:07,932 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:service=EJB3,ear=ysf.ear,jar=ysf.jar,name=Employee with dependencies:
            08:55:08,533 INFO [EJB3Deployer] Deployed: file:/J:/jboss-4.0.3SP1/server/all/tmp/deploy/tmp33605ysf.ear-contents/ysf.jar
            08:55:08,543 INFO [TomcatDeployer] deploy, ctxPath=/ysf, warUrl=.../tmp/deploy/tmp33605ysf.ear-contents/ysf-exp.war/
            08:55:09,705 INFO [EARDeployer] Started J2EE application: file:/J:/jboss-4.0.3SP1/server/all/deploy/ysf.ear

            And this is part of my source code:

            EmployeeSessionLocal.java

            @Local
            public interface EmployeeSessionLocal
            { ... }

            EmployeeSessionBean.java

            @Stateless(name="Employee")
            public class EmployeeSessionBean implements EmployeeSessionLocal
            { ... }

            Controller.java

            public class Controller extends HttpServlet {
            public void doGet( ... ) throws ServletException, IOException
            {
            InitialContext context = new InitialContext();
            EmployeeSessionLocal session = (EmployeeSessionLocal) context.lookup( "Employee" );
            ...
            }
            }

            Any idea?

            Thanks!!
            José Luis.

            • 3. Re: Accessing an EJB from a Servlet
              Peter Voringer Newbie

              with 4.0.3SP1 use (EmployeeSessionLocal) context.lookup(EmployeeSessionLocal.class.getName());

              • 4. Re: Accessing an EJB from a Servlet
                José Luis Laviña Newbie

                Thank you echon for your suggestion, but it doesn't work.
                (I have replaced the @Stateless(name="Employee") annotation with only @Stateless).

                13:24:04,806 INFO [STDOUT] javax.naming.NameNotFoundException: es.dbs.ysf.employee.EmployeeSessionLocal not bound
                13:24:04,816 INFO [STDOUT] at org.jnp.server.NamingServer.getBinding(NamingServer.java:514)
                13:24:04,816 INFO [STDOUT] at org.jnp.server.NamingServer.getBinding(NamingServer.java:522)
                13:24:04,816 INFO [STDOUT] at org.jnp.server.NamingServer.getObject(NamingServer.java:528)
                13:24:04,816 INFO [STDOUT] at org.jnp.server.NamingServer.lookup(NamingServer.java:281)
                13:24:04,816 INFO [STDOUT] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
                13:24:04,816 INFO [STDOUT] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
                13:24:04,816 INFO [STDOUT] at javax.naming.InitialContext.lookup(InitialContext.java:351)
                13:24:04,816 INFO [STDOUT] at es.dbs.ysf.employee.Controller.doGet(Controller.java:28)

                Perhaps if I use a remote interface instead of a local one.. no, it doesn't work either.

                Nobody has an example? Please?

                Thanks!
                José Luis.

                • 5. Re: Accessing an EJB from a Servlet
                  José Luis Laviña Newbie

                  I have installed jboss-4.0.4.CR2 but the same problem persists.

                  I don't know why... it seems that the deployment works fine, but when I trying to access to the local EJB3 from my Servlet the JNDI doesn't find it... :(

                  • 6. Re: Accessing an EJB from a Servlet
                    José Luis Laviña Newbie

                    Using this code it seems that the JNDI founds the EJB3 (but crash searching a resource, but this is another story...):

                    import org.jboss.annotation.ejb.LocalBinding;

                    @Stateless
                    @Local( { EmployeeSessionLocal.class } )
                    @LocalBinding( jndiBinding="Employee" )

                    How can I define the jndiBinding without using a jboss especific class like 'org.jboss.annotation.ejb.LocalBinding'?

                    Thanks!
                    José Luis.

                    • 7. Re: Accessing an EJB from a Servlet
                      Pat Osterday Newbie

                      I haven't had any luck using the class.getName() lookup either. The only way I got it to work is using sm = (SiteManager)ic.lookup("SiteManagerImpl/local"); - which I can see in the Global JNDI Namespace. (ic being my InitialContext variable, SiteManager being the stateless session bean interface, and SiteManagerImpl being the implementation.) In the EJB3Trail sample app, this is how they are doing it in the JSPs.

                      • 8. Re: Accessing an EJB from a Servlet
                        Alexander Sack Novice

                         

                        "jllavina" wrote:
                        Using this code it seems that the JNDI founds the EJB3 (but crash searching a resource, but this is another story...):

                        import org.jboss.annotation.ejb.LocalBinding;

                        @Stateless
                        @Local( { EmployeeSessionLocal.class } )
                        @LocalBinding( jndiBinding="Employee" )

                        How can I define the jndiBinding without using a jboss especific class like 'org.jboss.annotation.ejb.LocalBinding'?

                        Thanks!
                        José Luis.


                        Jose, this is a lacking feature of the EJB3 spec. I have formally written the jsr-220 group to give my opinion that jndi binding should be standardized throughout all implementations.

                        • 9. Re: Accessing an EJB from a Servlet
                          José Luis Laviña Newbie

                          Well, finally it works (using jboss-4.0.3SP1 and jboss-4.0.4.CR2 too)!!

                          Here is the code:

                          @Local
                          public interface EmployeeSessionLocal {
                          ...
                          }


                          @Stateless ( name="Employee" )
                          public class EmployeeSessionBean implements EmployeeSessionLocal {
                          @Resource ( mappedName="java:/jdbc/OracleERPDS" )
                          private DataSource ds;
                          ...
                          }


                          public class Controller extends HttpServlet {
                          public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
                          InitialContext context = new InitialContext();
                          EmployeeSessionLocal session = (EmployeeSessionLocal) context.lookup( "ysf/Employee/local" );
                          ...
                          }
                          }


                          Thanks to everybody!!
                          José Luis.