4 Replies Latest reply on Dec 11, 2004 2:50 AM by espenra

    EJB3 and Servlets

    espenra Newbie

      Are there any tutorials on how to run a servlet with EJB 3.0 on JBoss?

        • 1. Re: EJB3 and Servlets
          Bill Burke Master

          Should be the same as EJB 2.1.

          Bill

          • 2. Re: EJB3 and Servlets
            espenra Newbie

            Well there is some difference to the deployment descriptors. I have mostly tried forward and backward, and ended up with something like this to test with. It will deploy without errors, but I got no clue what the url is like. I have tried out http://localhost:8080/stateless/hello for no good.

            Any1 that see my error?


            web.xml

            <web-app>
            
             <ejb-server>
             <jndi-name>java:comp/env/ejb</jndi-name>
             <bean-type>HelloBean</bean-type>
             </ejb-server>
            
             <servlet>
             <servlet-name>hello</servlet-name>
             <servlet-class>HelloServlet</servlet-class>
             </servlet>
            
             <servlet-mapping>
             <servlet-name>hello</servlet-name>
             <url-pattern>/stateless</url-pattern>
             <servlet-class>HelloServlet</servlet-class>
             </servlet-mapping>
            </web-app>


            Hello.java

            /**
             * Interface for the Hello session.
             */
            public interface Hello {
             /**
             * Returns a hello, world string.
             */
             public String hello();
            }


            HelloBean.java

            import javax.ejb.Stateless;
            import javax.ejb.Inject;
            
            
            /**
             * Implementation of the Hello bean.
             */
            @Stateless
            public class HelloBean implements Hello {
             private String _greeting = "Default Hello";
            
             /**
             * Injector to set the greeting.
             */
             @Inject
             public void setGreeting(String greeting)
             {
             _greeting = greeting;
             }
            
             /**
             * Returns a hello, world string.
             */
             public String hello()
             {
             return _greeting;
             }
            }


            HelloServlet.java

            import java.io.IOException;
            import java.io.PrintWriter;
            
            import javax.servlet.GenericServlet;
            import javax.servlet.ServletException;
            
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
            
            import javax.ejb.EJB;
            
            public class HelloServlet extends HttpServlet {
             private Hello _hello;
            
             /**
             * Dependency injector for the Hello interface.
             */
             @EJB(name="HelloBean")
             public void setHello(Hello hello)
             {
             _hello = hello;
             }
            
             public void service(HttpServletRequest req, HttpServletResponse res)
             throws IOException, ServletException
             {
             PrintWriter out = res.getWriter();
            
             if (_hello == null) {
             out.println("This example requires JDK 1.5");
             return;
             }
            
             out.println(_hello.hello());
             }
            }


            • 3. Re: EJB3 and Servlets
              Bill Burke Master

              look up how to do ejb-refs. I don't know if it will work, but just put the remote/local interface in the local-home/remote-home tags...If all else fails, just look up the beans in JNDI directly.

              new InitialContext().lookup(YouBeanRemote.class.getName());

              or

              InitialContext().lookup(YouBeanLocal.class.getName());

              • 4. Re: EJB3 and Servlets
                espenra Newbie

                I skipped your first advice, and did your last one

                new InitialContext().lookup(YouBeanRemote.class.getName());

                and it worked!

                Thanx