8 Replies Latest reply on May 18, 2009 12:47 AM by mikewse

    simplest possible servlet app

    mikewse

      I'm trying to put together a really small example using WebBeans RI in a basic servlet app. WebBeans kicks in but doesn't instantiate MyService for me (null in doGet). Here's the code:



      public class MyServlet extends HttpServlet {
           @Current MyService service;
           protected void doGet(...) ... {
                response.getWriter().print(service.getStuff());
           }
      }
      
      @ApplicationScoped
      public class MyService {
           ...
      }
      
      WEB-INF/web.xml:
           <servlet>
           ...
           <servlet-mapping>
           ...
           <listener>
                <listener-class>
                     org.jboss.webbeans.environment.servlet.Listener
                </listener-class>
           </listener>
      
      WEB-INF/lib:
           webbeans-servlet.jar
      



      Obviously, I'm missing something...?

        • 1. Re: simplest possible servlet app
          hcgpragt

          my first thought is: does it have a @name?




          No Name, no game is the theme in seam

          • 2. Re: simplest possible servlet app
            mikewse

            If you mean @Named on MyService, it doesn't seem to make a difference?


            BTW, here's the WebBeans stuff from the startup log:


            INFO: Starting Servlet Engine: Apache Tomcat/6.0.18
            ...
            INFO: Web Beans 1.0.0.CR1
            INFO: Transactional services not available.  Transactional observers will be invoked synchronously.
            INFO: EJB services not available. Session beans will be simple beans, injection into non-contextual EJBs, injection of @EJB in simple beans, injection of Java EE resources and JMS resources will not be available.
            INFO: JPA services not available. Injection of @PersistenceContext will not occur. Entity beans will be discovered as simple beans.
            



            • 3. Re: simplest possible servlet app
              nickarls

              Looking at https://jira.jboss.org/jira/browse/WBINT-5 I would guess it's not yet supported (at least not on tomcat)

              • 4. Re: simplest possible servlet app
                mikewse

                Right, yes.


                Also, I saw this mention in the refguide :




                Web Beans also supports Servlet injection in Tomcat. To enable this, place the webbeans-tomcat-support.jar in $TOMCATHOME/lib, and add the following to your META-INF/context.xml:

                <Listener className="org.jboss.webbeans.environment.tomcat.WebBeansLifecycleListener"> </Listener>
                




                I guess there is some extra appserver magic needed to support servlet injection, therefore the special tomcat support file, so I'll stay away from that to keep my app simple and servlet container-agnostic...


                What is the simplest way to get started with some injections in a pure servlet app? I tried to move my injection out from MyServlet to another class, which is new:ed from MyServlet.doGet, but I still only get null...?


                mvh Mike (Mölnlycke ;-)






                • 5. Re: simplest possible servlet app
                  gavin.king

                  I tried to move my injection out from MyServlet to another class, which is new:ed from MyServlet.doGet, but I still only get null...?

                  Injection doesn't apply to objects you instantiate using new. You have to obtain the object from the container, using BeanManager.getInstance().

                  • 6. Re: simplest possible servlet app
                    mikewse

                    Gavin King wrote on May 17, 2009 08:24:


                    Injection doesn't apply to objects you instantiate using new. You have to obtain the object from the container, using BeanManager.getInstance().

                    I was thinking about that route too, but the examples in the refguide suggest that I should have a manager instance injected by WebBeans like this:



                    @Current Manager manager;




                    which doesn't work as I don't have any contextual bean instance yet that can receive this injection.


                    Looking at the spec I see that looking up the Manager using JNDI is an alternative way. Is that my only option in this case?

                    • 7. Re: simplest possible servlet app
                      gavin.king

                      Until the functionality in WBINT-5 is implemented, yes.

                      • 8. Re: simplest possible servlet app
                        mikewse

                        I dug around a little in the sources, and am now successfully using


                        Manager manager = CurrentManager.rootManager();



                        to get the manager programatically. This ties my code to the JBoss RI but on the other hand lets me skip adding any container-specific JNDI setup (Tomcat's context.xml), which suits my demo examples fine.