Discussions

EJB programming & troubleshooting: what is the advantage of using ejb-ref in the web.xml file

  1. Hi folks,

    what is the advantage of using ejb-ref in the web.xml in the .war file? will app server load all referred EJB while loading the web application?

    I believe i can call any EJB from my web app thro' JNDI.

    Then what does matter providing the ejb-ref in the web.xml ?

    Thanks
  2. The advantage is indirection. Coding direct references to EJB's in your servlets through JNDI will work provided the beans JNDI name does not change during deployment.

    By using an ejb-ref you create an alias for the bean. During coding this alias is used to locate the beans home.

    eg. context.lookup("java:comp/env/ejb/myBean");

    The developer also creates an entry in the web.xml like this:

    <ejb-ref>
      <ejb-ref-name>ejb/myBean</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <ejb-ref-home>some.package.HomeInterface</ejb-ref-home>
      <ejb-ref-remote>some.package.RemoteInterface</ejb-ref-remote>
    </ejb-ref>

    On deployment the deployer creates a mapping between the alias ejb/myBean and the JNDI name the bean is actually deployed with. Mapping is vendor specific feature. For example, in WebLogic this is done in the weblogic.xml file.

    Why do it this way? Becuse it allows the deployer to change a beans JNDI name without recompiling the servlet (or having the source code).

    Regards,
    Stephen