6 Replies Latest reply on Feb 1, 2005 1:27 PM by Denis Souza

    Web apps and stateful beans

    Denis Souza Newbie

      Hi,

      I have a stateful ejb running in jboss and two web apps running in tomcat. The two web apps are a product catalog and a shopping cart for an online store. The shopping cart uses the stateful ejb to keep track of it's contents. However, I'd like to have the product catalog access the same bean to retrieve the cart's contents and display it in other parts of the store but whenever I look up the bean on JNDI, I get a brand new, and of course, empty, shopping cart stateful bean.
      Is there any way that I can access the exact object that the shopping cart is using?

        • 1. Re: Web apps and stateful beans
          Lothar Egger Newbie

          hi dsouza, a quick solution?!

          you could get the beanhandle and pass it to the servlet and call
          beanhandle.getEJBObject().
          ...
          javax.ejb.Handle carthandle = cartBean.getHandle()

          put it into the session -> ...

          on the other side (servlet)

          javax.ejb.Handle carthandle = (Handle) session.getAttribute(.....
          Cartbean cartBean = (Cartbean) carthandle.getEJBObject();

          my 2 cents...
          cheers
          L





          • 2. Re: Web apps and stateful beans
            Denis Souza Newbie

            Hi, from what I could understand from your solution you are assuming both apps have access to the same session object. Perhaps I wasn't very clear... let me explain in greater detail.

            I created the two web apps in jsf. Each of them has a backing bean with a session scope, which basically means that each user who accesses each app will have their own backing bean object.
            One of these web apps is a shopping cart app in which the user will see the items he added to the shopping cart and will be able to change the quantities and remove the items. The information as to what is in the shopping cart is not stored on the web app's backing bean. It's kept in a stateful ejb running in a jboss container.
            When the user first accesses the shopping cart, the newly created backing bean will check if it already has a reference to remote stateful ejb. If it doesn't it looks it up in JNDI to obtain one and, afterwards the same reference obtained is used in subsequent requests from that application.

            The other application needs to access the same information as the shopping cart app, that is, the information that is stored in the stateful ejb, present in the jboss container. Since this app is not the shopping cart (and may well be running on a different machine), it doensn't have access to any info present in the shopping cart app's backing beans, which means it doensn't have access to the reference of the stateful ejb that the shopping cart app is using.
            If this other app, which also has it's own backing beans, looks up the stateful ejb in JNDI, it receives an unused instance of the bean. What I need is to have access to the same instance as the shopping cart app has.

            Any ideas?

            • 3. Re: Web apps and stateful beans
              Lothar Egger Newbie

              hello again

              when the other application knows nothing about the cart (or many carts..)
              and could not be invoked by the cart.

              the cart could store the info of his SFSB in an entity bean in the database?
              the "other application" could query the entity bean and find the already created SFSB's

              cheers
              L

              • 4. Re: Web apps and stateful beans
                Denis Souza Newbie

                Yes, that's a good solution, another would be to keep that information in a cookie. But my problem is not exactly how to know which SFSB to look for.
                What I can't figure out is how do "find" the already created SFSB?
                So let's suppose I already have the info needed to find it. How do I do it? I can't just look it up through JNDI, right? If I do that, I'll end up with a new SFSB.

                thanks!

                • 5. Re: Web apps and stateful beans
                  Lothar Egger Newbie

                  hi

                  ok...
                  as in my first post ;)

                  lets say your cart servlet (or whatever) has created the SFSB
                  lets call it cartBean
                  now you get the Handle of the bean with..

                  javax.ejb.Handle carthandle = cartBean.getHandle()

                  now transport the "carthandle" to the "other application"
                  in the "other application" do
                  Cartbean cartBean = (Cartbean) carthandle.getEJBObject();
                  and voila...

                  you can even make a string from the handle (handlestring=carthandle.toString())
                  and pass it thru session,urlparameter,read it from entity bean

                  and with
                  javax.ejb.Handle carthandle =(Handle) handlestring;
                  Cartbean cartBean = (Cartbean) carthandle.getEJBObject();

                  you will get the already created Bean in the other application.
                  (afaik this works only if the bean is in the same server)

                  afaik it should also works with the remote home (hajndi) if the bean is on another server....


                  sorry for the confusion..
                  i am not good in explaining things *smiles*

                  cheers
                  L







                  • 6. Re: Web apps and stateful beans
                    Denis Souza Newbie

                    Great! I get it now!
                    Thanks a lot!