Version 3

    Question

    I have a web application through which i want to access an EJB which is deployed as a separate appliaction on the same server. How do i do it?

     

    Answer

     

    In the web.xml of your war, make the following entry:

     

     

     

    <ejb-ref>     <ejb-ref-name>GiveAnyNameByWhichYouWouldLikeToReferTheBeanInYourWebApp</ejb-ref-name>     <ejb-ref-type>session</ejb-ref-type>     <home>packageName.ClassNameOfTheHomeObjectOfTheBeanYouWantToRefer</home>     <remote>packageName.ClassNameOfTheRemoteObjectOfTheBeanYouWantToRefer</remote>   </ejb-ref>

     

    In the jboss-web.xml of your war, make the following entry:

     

     

     

    <ejb-ref>   <ejb-ref-name>GiveAnyNameByWhichYouWouldLikeToReferTheBeanInYourWebApp(This should be same as the one given in the web.xml above)</ejb-ref-name>   <jndi-name>TheJndiNametoWhichTheBeanIsBound(YouWillFindThisJndiNameInTheJboss.xmlOfTheEJB)</jndi-name> </ejb-ref>

     

    In your code, do the lookup as:

     

     

     

        Context ic = new InitialContext();   Object ejbHome = ic.lookup("java:comp/env/TheNameThatYouHadGivenInTheEJB-REF-NAMETagOfJbossWeb.xmlAbove");

     

    Here's an example:

     

     

     

    web.xml:

     

     

      <ejb-ref>     <ejb-ref-name>MyTestBean</ejb-ref-name>     <ejb-ref-type>session</ejb-ref-type>     <home>com.test.ejb.MyBeanHome</home>     <remote>com.test.ejb.MyBeanRemote</remote>   </ejb-ref>

     

    jboss-web.xml:

     

     

    <ejb-ref>   <ejb-ref-name>MyTestBean</ejb-ref-name>   <jndi-name>myejb/test/MyTestBean</jndi-name> </ejb-ref>

     

    Lookup code:

     

     

         Context ic = new InitialContext();         Object ejbHome = ic.lookup("java:comp/env/MyTestBean");  

     

     

    Referenced by: