5 Replies Latest reply on Apr 2, 2013 1:53 AM by Nicklas Karlsson

    EJB3 injection in servlet with JBoss AS 7

    evgeniy3054 Newbie

      Hi everybody!! I have enterprise application which includes ejb(jar) and web(war) modules and I want to use EJB3 stateless bean in web servlet. When i try to use @EJB or @Inject annotations for injecting EJB3 component in servlet, i get 'null'.  Can i use @EJB or @Inject in this case? Are there any ways for injecting ejb with annotations instead of simply new InitialContext().lookup? THX!!

        • 1. Re: EJB3 injection in servlet with JBoss AS 7
          Stephen Coy Master

          Hi there, and welcome to the JBoss forums!

           

          Can you show us the code that is not working?

          • 2. Re: EJB3 injection in servlet with JBoss AS 7
            xiang yingbing Master

            I have same question too.

            Now, I try my best to use JSF MBean instead of Servlet (Actually, JSF MBean is Servlet too).

            • 3. Re: EJB3 injection in servlet with JBoss AS 7
              Dirk Gerrit Oort Newbie

              Hi all,

               

              Just tried it out and on a 7.1 jboss server it works. I created a new maven project in eclipse (jboss tools) with jboss-javaee6-ear-blank-archetype (version 7.1.3.CR3.

               

              add my servlet to the ....-web project

              add my ejb to the ....-ejb project

               

              I must added a dependency to the ...web project pom :

                  <dependency>
                  <groupId>javax.ejb</groupId>
                  <artifactId>ejb-api</artifactId>
                  <version>3.0</version>
                  </dependency>

               

               

              and my servlets and ejb works

               

               

              servlet code :

               

              package org.jboss.servlet2.servlet;

               

              import java.io.BufferedReader;

              import java.io.BufferedWriter;

              import java.io.IOException;

               

              import javax.ejb.EJB;

              import javax.servlet.ServletException;

              import javax.servlet.annotation.WebServlet;

              import javax.servlet.http.HttpServlet;

              import javax.servlet.http.HttpServletRequest;

              import javax.servlet.http.HttpServletResponse;

               

              import org.jboss.servlet2.ejb.CalcEJB;

               

               

               

               

               

              /**

              * Servlet implementation class Calc

              */

              @WebServlet("/Calc")

              public class Calc extends HttpServlet {

                  private static final long serialVersionUID = 1L;

                 

                  @EJB

                  private CalcEJB ejb;

                 

                  /**

                   * @see HttpServlet#HttpServlet()

                   */

                  public Calc() {

                      super();

                      // TODO Auto-generated constructor stub

                  }

               

                  /**

                   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

                   */

                  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                      response.setStatus(HttpServletResponse.SC_ACCEPTED);

                      try(BufferedWriter wr = new BufferedWriter(response.getWriter())){

                         

                          wr.append("<p>sum(1+2 )= " + ejb.sum(1,2)+ " <br/></p>");

                         

                         

                      }catch(IOException ioEx){

                          ioEx.printStackTrace();

                      }

                  }

               

                  /**

                   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

                   */

                  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

                      // TODO Auto-generated method stub

                  }

               

              }

               

              ejb code:

               

              package org.jboss.servlet2.ejb;

               

              import javax.ejb.LocalBean;

               

              import javax.ejb.Stateless;

               

              @Stateless

              @LocalBean

              public class CalcEJB {

               

                  public int sum(int...numbers){

                      if(numbers == null){

                          return 0;

                      }

                      int result = 0;

                      for(int number:numbers){

                          result+=number;

                      }

                      return result;

                  }

                 

              }

               

               

              so what is the difference between my code and yours?

              1 of 1 people found this helpful
              • 4. Re: EJB3 injection in servlet with JBoss AS 7
                evgeniy3054 Newbie

                Hi all! Thx very much for your reply. Your code is working) Sorry, but my question was not full. When i try to use inject ejb in class which extends HttpServlet - it is working good, but when i use struts2 actions is not working.I suppose that strats2 action is not related with HttpServlet. If this true, can we use @EJB ot  @Inject?

                • 5. Re: EJB3 injection in servlet with JBoss AS 7
                  Nicklas Karlsson Master

                  Struts doesn't have injection so it won't work there. I guess you could still access the CDI BeanManager from JNDI and get to a bean from there.