views:

143

answers:

1

I've been struggling for this for a while now.

I'm trying to gear up for EJB 3.0. I'm using JBoss 5.1.0 GA as my application server. I started with very simple stateless session bean with local interface and a simple jsp-servlet client which invokes session bean method. All this while I've been trying to use @EJB annotation to inject session bean into the servlet class.

public class SampleServlet extends HttpServlet {

    @EJB
    private PersonLocal person;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("In servlet");       
        response.getWriter().write("Person Name : "+person.getName());
        System.out.println(person.getName());       
    }
}

I'm using JBoss 5.1.0 GA with the default configuration. (I also tried with all configuration)

However, I used to get a null reference to the session bean injection. After struggling for a day or so, I finally tried the ugly EJB 2.x JNDI lookup method instead of @EJB annotation with configuration for jndi specified in the jndi.properties file and it worked without any other changes!

Now, I tried to find out in the JBoss docs whether JBoss 5.1.0 GA does or does not support the injection with @EJB annotation, but couldn't find a concrete answer. So can someone tell me whether it does? cause I would really prefer annotation over JNDI lookup (I mean, who will not?). Am i missing something..?

Probably should have put this in the JBoss forums, but.. I'm addicted to this place ;-)

+3  A: 

This is definitely supported by JBoss 5.x since it is Java EE 5 certified.

Actually, I suggest to check the Chapter 11. Introduction to EJB injection in Servlets of the JBoss EJB3 Tutorials, they describe a detailed and step by step example.

And pay a particular attention to the following note:

For the injection to take place in a web module, your web.xml should use the 2.5 version of the web-app xsd:

<web-app 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;
Pascal Thivent
I wonder how many servlet/jsp questions on SO have been solved by fixing the `web-app version`.....
skaffman
That fixed it! :) I should have read the tutorials before diving in,but anyways.. I learnt a good lesson.Thanks a lot! :)
Nikhil Patil
Are there any other gotacha's like these when using normal Java clients as well? Or we can inject in Java clients as is?
Nikhil Patil