3 Replies Latest reply on Mar 23, 2006 9:26 PM by treespace

    @EJB annotation not working in web tier

    treespace Novice

      Trying to use variable "server" throws a null pointer exception.

      public class Client
      {
       @EJB Server server;
      
       execute(String operation)
       {
       server.execute(operation);
       }
      }
      


      Does @EJB lose its magic power in the web tier?

        • 1. Re: @EJB annotation not working in web tier
          Alexander Sack Novice

           

          "treespace" wrote:
          Trying to use variable "server" throws a null pointer exception.

          public class Client
          {
           @EJB Server server;
          
           execute(String operation)
           {
           server.execute(operation);
           }
          }
          


          Does @EJB lose its magic power in the web tier?


          If you mean by web tier, servlets, then yes, @EJB does not exist at the Servlet level as of 2.4. It is one of the new features of 2.5 which JBoss doesn't support yet. I believe this bit is in the trailblazer/EJB3 doc (though I admit its kinda hidden). You still need to do JNDI lookups from a servlet to get accesss to the EJB tier.

          • 2. Re: @EJB annotation not working in web tier
            treespace Novice

            Good to hear. That annotation is facinating. In general terms, is shows you can use annotations to override Java's default variable initialization value in any reference. In this case to supply a container-specific value.

            • 3. Re: @EJB annotation not working in web tier
              treespace Novice

              Here's a tip for doing a lookup without having to cast: generic method. I've hard-coded some assumptions that are easy enough to parameterize or use globa settings with.


              
              public static <T> T getEJB(Class<T> klass) throws NamingException
              {
               return (T) (new InitialContext().lookup("app/" + klass.getSimpleName() + "Bean/local"));
              }
              
              Example use: PizzaService service = getEJB(PizzaService.class);