This article will teach you how you can look up an EJB (inside a JAR file) from a Web application (inside a WAR file).
Our scenario
The following picture depicts our Deployment scenario:
Firstly, in order to lookup an EJB packaged as JAR file from another application, for example a Web application you can use the @EJB annotation as follows:
@EJB(name="ejb:/ejb-server-basic/CalculatorEJB!com.mastertheboss.ejb.Calculator") Calculator calculator; @EJB(name="ejb:/ejb-server-basic/AccountEJB!com.mastertheboss.ejb.Account?stateful") Account account;
In the above example, we are referencing the Calculator Remote interface, whose implementation is CalculatorEJB, available in the application named ejb-server-basic.
Besides, your Web application needs to include in the pom.xml (or gradle.build) the EJB interfaces in order to be able to solve the dependencies.
A more detailed example is available here: WildFly: How to call an EJB from an EJB located in another application
Referencing EJBs through deployment descriptors
If you are coding in EJB2 style (which requires a Home and Remote interface for an EJB), what you need to do is adding the appropriate references to your web.xml and jboss-web.xml.
So if you have an EJB named “HelloBean” add to your web.xml
<ejb-ref> <ejb-ref-name>HelloBean</ejb-ref-name> <ejb-ref-type>session</ejb-ref-type> <home>test.HelloBeanHome</home> <remote>test.HelloBeanRemote</remote> </ejb-ref>
Then modify jboss-web.xml by adding:
<ejb-ref> <ejb-ref-name>HelloBean</ejb-ref-name> <jndi-name>HelloBean</jndi-name> </ejb-ref>
Now you can safely lookup your EJB from your Servlet:
Context ic = new InitialContext(); Object ejbHome = ic.lookup("java:comp/env/HelloBean");