5 Replies Latest reply on Aug 24, 2009 7:38 AM by Wolfgang Knauf

    Problem with EJB Injection in Servlet

    Sarabjit Dua Newbie

      Hey Guys,

      I know there is another similar thread but i couldn't find an answer there. It might be because am really new to EJB so well, please bear with me.

      I have a war and an ejb.jar which i package in an ear. I have 2 questions

      1. Am not able to access the ejb from the servlet. Can someone tell me what all configurations are needed in which all descriptors.

      2. I've put shared libraries directly under the ear but they are not visible to my war. How can i fix that?

      My ear structure is

      EAR
      |->Meta-inf
      |->application.xml (defines the two ejb and war modules)
      |->jboss-app.xml (empty)
      |->my war.war
      |->WEB-INF
      |->web.xml (no declaration of ejb here)
      |->myejb.jar
      |->META-INF
      |->jboss.xml (which is basically empty - just usual xml markup)
      |->ejb-jar.xml(which is empty too)
      |->all shared libraries


      I have a servlet in my war

      TestServelet.java

      public class TestServlet extends HttpServlet {

      @EJB
      MySessionBean mybean;

      @Override
      protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      system.out.println(mybean);
      }

      }

      I have an interface in myejb.jar

      MySessionBean.java

      public interface MySessionBean{
      void myFunc();
      }


      MySessionBeanImpl.java

      public class MySessionBeanImpl Implements MySessionBean{
      public void myFunc(){
      system.out.println("Bean Called");
      }
      }

      Am using jboss 4.2.3. Servelet gives me an npe at system.out so the injection isn't working.

      Any help on this would be really awesome.







        • 1. Re: Problem with EJB Injection in Servlet
          Sarabjit Dua Newbie

          My ear structure is

          EAR
          |->Meta-inf
          |-------|->application.xml-(defines-the-two-ejb-and-war-modules)
          |-------|->jboss-app.xml-(empty)
          |->my-war.war
          |-------|->WEB-INF
          |--------------|->web.xml-(no-declaration-of-ejb-here)
          |->myejb.jar
          |-------|->META-INF
          |--------------|->jboss.xml-(which-is-basically-empty---just-usual-xml-markup)
          |--------------|->ejb-jar.xml(which-is-empty-too)
          |->all-shared-libraries

          • 2. Re: Problem with EJB Injection in Servlet
            jaikiran pai Master

            If you are starting with EJB3 then i would recommend you to use the latest AS5 version (i.e. 5.1.0) because it has better spec compliant support for EJB3.

            Injection of EJB3 in servlets isn't supported in 4.2.x for reasons mentioned in our Sticky here http://www.jboss.org/index.html?module=bb&op=viewtopic&t=107353


            I've put shared libraries directly under the ear but they are not visible to my war. How can i fix that?


            Place those common jars in the EAR/lib folder.


            • 3. Re: Problem with EJB Injection in Servlet
              Sarabjit Dua Newbie

              Hey Jaikiran,

              Thanks for your reply. I tried using the same annotation in JBoss 5.1 but it gives the same error. I've now used pitchfork to support annotations in my servlets and web modules.

              One more quick question about shared libraries here. I've put my shared libs directly under ear. And the libs specific to war in WEB-INF/lib

              myear.ear
              |____myjar.jar
              |____mywar.war
              ............|___WEB-INF
              .....................|__lib
              ...........................|__mywarlib.jar

              Manifest.mf in mywar.war is
              Class-Path: myjar.jar mywarlib.war


              My war is able to access files in myjar.jar but it gives a NoClassDefFound for all classes n mywarlib.war

              Can you please tell me how i can specify relative paths in manifest?

              • 4. Re: Problem with EJB Injection in Servlet
                Sarabjit Dua Newbie

                btw i already tried changing manifest to

                Class-Path: myjar.jar WEB-INF/lib/mywarlib.war

                • 5. Re: Problem with EJB Injection in Servlet
                  Wolfgang Knauf Master

                  Hi,

                  the JAR files in WEB-INF\lib are automatically added to the classpath, no need to add those to META-INF\Manifest.mf.

                  As you are using EJB3, you don't need any deployment descriptors besides web.xml, if you use annotations. You might try to remove them all, just to check that no error in one of them breaks your app.

                  You might post the relevant parts of your servlet code, which don't work. And what is the error message?

                  Best regards

                  Wolfgang