3 Replies Latest reply on Dec 22, 2003 2:31 AM by jonlee

    jsp's and ejb

    vaat

      I have a stateless session bean which is performing some business logic on the cmp entity beans. The session bean returns results in a Vector or a String.

      I use a servlet controller to intercept requests from the jsp's and ask's the session bean for the results. The servlet is putting the Vector or String as a attribute in the request object.

      In the jsp's I get this attribute and cast it to a vector or String and displays the results.

      My question:

      Is it a better design if I wrap lookup's on the session bean and the results from that session bean in a javabean wrapper. Each javabean wrapper class is also representing an row in the database then, like the entity bean.

      Or should the session bean always be returning a vector, even if it is returning just one Object. This way I could make a jsp which displays the results in a tabel. I could then include that jsp in all the outputjsp's.

      I am also just returning some of the attribute's of a entity bean in the result. It could be possible that I simply want to change that in the future.

      I ask this question because I want the minimum of java logic in my jsp, so you need not to know it is even working with ejb's ! The MVC pattern in use ....

        • 1. Re: jsp's and ejb
          jonlee

          This is a difficult question to answer as it is subjective and conditional upon so many conditions. While using a JavaBean proxy implementation hides much of the mechanics of using an EJB, you need to balance this with the usage.

          JNDI lookups are expensive and this can degrade your application performance if your proxy performs this lookup on every invocation. Caching the lookup brings performance benefits but introduces complexities that may impact the ability to use your JavaBean.

          From the perspective of generalised result access, the use of collections reduce the number of specialized methods -> returns and can minimize maintenance.

          YMMV. Patterns are useful but only if you can assess all the forces and environmental conditions to which your solution will be subjected, and determine the best pattern combination for the conditions.

          • 2. Re: jsp's and ejb
            vaat

            ok,

            but i could do a lookup on the stateless session bean in de init method of the servlet, right ? And just create a instance of the session bean on the home object on every method invociation (not meaning it actually will be created) !!??

            I don't like the javabean wrappers actually, because it will take time to create them. I'm wondering what would be the best approach in your opinion.

            You could also use struts and perform the lookup's in the action Object. This seems a good approach, if you want to keep your jsp's simple.

            But javabeans are keeping the jsp's simple too, but it takes more work to develop them.
            Giving a Vector back at all times, is keeping my jsp's simple too, because people who are designing the jsp's just have to include the jsp, which is giving back the results's.
            But giving back a Vector, where you know that there will be just one object in it at all times, seems a bit strange. Besides giving back a String should be easer to marshal trough to stubs and skeleton's ??


            • 3. Re: jsp's and ejb
              jonlee

              Yes. Using the init or jspInit caches the home reference you have looked up. This is the usual approach for servlets and JSPs. However, it does suffer from a problem when the EJB is redeployed as the reference is no longer valid. You will also need to perform some sort of caching of the CMP bean home reference used by the session bean if you want to improve the efficiency there.

              Using a collection means that should the function mutate and it may be possible to return more than one value, that there is minimal (no changes) to the client code. The downside is iterating through a collection. As I said, YMMV and only the designer can make decisions on this. Performance might be better with transporting only a String but usage will determine the actual performance impact. As with everything, it is a matter of tradeoffs dependent on your situation.

              Hope that helps.