title: EJB injection in servlet fails
body: <p>I am trying to inject an EJB stateless bean in to a servlet, but the servlet throws a NullPointerExcetion. I am using JBOSS to deploy the EJB and servlet.</p>
  
  <p>I am relatively new to the Java world, so I am posting the steps I followed.</p>
  
  <p><strong>Interface</strong></p>
  
  <pre><code>package MavenEJB.Bidding`
  import javax.ejb.Local;
  
  @Local
  public interface PlaceBid {
   public String AddBid();
  }
  </code></pre>
  
  <p><strong>Bean</strong></p>
  
  <pre><code>package MavenEJB.Bidding;
  import javax.ejb.Stateless;
  @Stateless(name="PlaceBid")
  public class PlaceBidBean implements PlaceBid {
   public PlaceBidBean(){}
  
   /**
    * Include logic to add the bid 
    */
  
   public String AddBid(){
    return "Placed bid using EJB"; 
   }
  }
  </code></pre>
  
  <p>I created a jar file of the bean using maven and I copied the jar file to "deploy" directory of JBOSS. I am able to see the bean deployed in the JMX console.</p>
  
  <p><strong>Global JNDI Namespace in JMX console</strong></p>
  
  <pre><code>+- PlaceBid (class: org.jnp.interfaces.NamingContext)
    |   +- local (proxy: $Proxy63 implements interface MavenEJB.Bidding.PlaceBid,interface org.jboss.ejb3.JBossProxy)
  </code></pre>
  
  <p>My servlet code</p>
  
  <pre><code>public class PlaceBidServlet extends HttpServlet {
  
      @EJB
      private PlaceBid placeBid;
  
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
  
    PrintWriter out = response.getWriter();
       out.println("&lt;HTML&gt;");
       out.println("&lt;HEAD&gt;&lt;TITLE&gt;Hello&lt;/TITLE&gt;&lt;/HEAD&gt;");
       out.println("&lt;BODY&gt;");
    out.println("Output from EJB" +placeBid.AddBid());
  
       //out.println("Output from EJB" );
       out.println("&lt;/BODY&gt;&lt;/HTML&gt;");  
   }
  
  ....
  }
  </code></pre>
  
  <p>When I point to the URL of my servlet, I get NullPointerException. When I comment the bean and print something else, it prints fine. So I am sure the problem is with the EJB Dependency Injection in the servlet. 
  I tried many solutions suggested else where, nothing really worked, someone please help me.</p>