title: EJB dependency injection into Servelt - WAS7
body: <p>I am trying to inject an EJB with the @EJB annotation :</p>
  
  <ul>
  <li>when I inject an EJB into another EJB it works fine.</li>
  <li>when I inject the same EJB into a servlet I got a null pointer exception (my EJB is null).</li>
  </ul>
  
  <p><em>myapp.ear</em> contains the following :</p>
  
  <ul>
  <li><em>myapp.war</em> (where the servlet is located)</li>
  <li><em>myapp.jar</em> (where the EJBs are)</li>
  </ul>
  
  <p>EJB Interface :</p>
  
  <pre><code>package com.mycompany.myapp.ejb.hello;
  
  @Local
  public interface HelloEjb {
      public final static String NAME = "HelloEjb";   
      public String sayHello();
  }
  </code></pre>
  
  <p>EJB Impl :</p>
  
  <pre><code>package com.mycompany.myapp.ejb.hello;
  
  @Stateless(name = HelloEjb.NAME)
  public class HelloEjbImpl implements HelloEjb {
  
      @Override
      public String sayHello() {
          return "Hello";
      }
  }
  </code></pre>
  
  <p>My Servlet :</p>
  
  <pre><code>public class HelloServlet extends HttpServlet {
  
      @EJB
      private HelloEjb helloEjb;
  
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
          resp.setContentType("text/html");
          PrintWriter out = resp.getWriter();
          out.println("&lt;html&gt;");
          out.println("This is my stateless session-EJB: " + helloEjb.sayHello());
          out.println("&lt;/html&gt;");
          out.close();
      }
  }
  </code></pre>
  
  <p>The web.xml in the WAR has nothing spetacular, only the servlet defintion, no ejb tags :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="MyApp" version="2.5"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- General --&gt;
      &lt;display-name&gt;My App&lt;/display-name&gt;
  
      &lt;!-- Standard Action Servlet Configuration --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;helloServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;com.mycompany.myapp.web.servlet.HelloServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Standard Action Servlet Mapping --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;helloServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;!-- The Usual Welcome File List --&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>If in the servlet I use lookup, it works I am able to get the EJB :</p>
  
  <pre><code>helloEjb = ctx.lookup("ejblocal:com.mycompany.myapp.ejb.hello.HelloEjb");
  </code></pre>
  
  <p>I am using <strong>Websphere 7</strong> &amp; <strong>EJB 3.0</strong></p>
  
  <p>Any help would be much appreciated.</p>