Servlets to EJB communications


Web tier: servlets, JSP, Web frameworks: Servlets to EJB communications

  1. Servlets to EJB communications (1 messages)


    Bear with me here. I am rather new to servlets and EJBs so I am pretty confused at the moment.

    First, let me say that I am posting here because I haven't found any thread that could answer my questions.

    I am trying to build an architecture as such:
    - client sends request to servlet
    - servlet decides which EJB(s) to service
      request and forwards the request to
      that EJB
    - EJB processes and sends response back to
    - servlet returns response via JSP
      Note: servlet may collate response from
            2 or 3 EJBs before it returns the
            response to the client

    1) Does the architecture that I described
       sound feasible?
    2) I am having trouble trying to forward
       the request from the servlet to the EJB.
       As I understand, this process may be
       helped by using embedding the EJBs into
       another JSP page. Am I correct? Are
       there any other ways of accomplishing
    3) One way that I thought which may be
       possible to solve this problem is :
       - use the servlet to choose the EJBs
       - embed the chosen EJBs into various
         frames in a JSP page
       Does this look like a more correct way
       to implement what I am trying to do?

    Please help, I am very confused.

    Thanks in advance,
  2. Servlets to EJB communications[ Go to top ]

    Your proposed use of a Servlet for taking the request, EJBs for doing the heavy work, and (back in the Servlet) selecting a JSP to present the view to the user is what I prefer to do. It looks like one design Sun recommends in their J2EE "Blueprints", which you can download from

    IBM has a similar design, although they don't talk about EJB. Check it out at

    Regarding your questions:
    1) yes
    2) Don't know. If by "forwarding the request from the Servlet to the EJB", you mean calling the EJB from the Servlet, that's what I do (although taking pains to minimize the number of calls, as they are expensive. You can have the EJB return a data object with any results you need to finish processing the request, and thereby make only a single EJB call from the Servlet). I don't really like calling the EJB tier from JSPs if I can help it, preferring to retrict JSPs to "presentation" (basic HTML with a few loop controls or calls to formatting code in regular JavaBeans components, not EJBs).

    But this is just a preference. Some people still divide their code into Model, View, and Controller... but they use a separate JSP for the View and another for the Controller. The "Controller" JSP would do the work of the Servlet. In that case, I would call the EJB tier from a Controller JSP but not from one I'd set aside for the presentation layer.
    However, I like using a Servlet instead just to help me keep the various components and their jobs mentally separate. Less confusing for someone new if they inherit something and want an idea of what a component does ("Oh, a Servlet. Probably is the initial point-of-entry, probably calls EJBs and then picks a JSP to show the results").

    3) Still stuck on the "embed the EJB" part. Sorry! What do you mean? You could always get a data object returned from your EJB call, and in the Servlet, pass it to your select JSP for presentation via the request or session objects (see setAttribute in both HttpServletRequest and session in the Servlet 2.2 spec).