<application>
<display-name>EJB3 Ear tutorial</display-name>
<module>
<web>
<web-uri>tutorial.war</web-uri>
<context-root>/tutorial</context-root>
</web>
</module>
<module>
<ejb>tutorial.jar</ejb>
</module>
</application>
public void init() throws ServletException
{
super.init();
try
{
InitialContext ctx = new InitialContext();
// J2EE 1.5 has not yet defined exact XML <ejb-ref> syntax for EJB3
CalculatorLocal calculator = (CalculatorLocal) ctx.lookup("tutorial/CalculatorBean/local");
setCalculator(calculator);
}
catch (NamingException e)
{
throw new RuntimeException(e);
}
}
One thing about EAR packaging is that the default JNDI name is recalculated to be EARNAME/EJBNAME/local for Local interfaces and EARNAME/EJBNAME/remote for remote interfaces.
To give you a preview of how injection will work inside servlets/jsps, we've simulated the code. Take a look at CalculatorActionServlet.java. You can use this pattern until the XML Schema is updated for J2EE 1.5 for injection, or until Tomcat supports EJB injection annotations.
private CalculatorLocal calculator;
/**
* The @EJB annotation is similated. Tomcat does not yet understand injection annotations.
* @param calculator
*/
@EJB(name = "calculator")
public void setCalculator(CalculatorLocal calculator)
{
this.calculator = calculator;
}
<persistence>
<persistence-unit name="testdb">
...
<properties>
<property name="entity.manager.factory.jndi.name" value="java:/MyEntityManagerFactory"/>
</properties>
</persistence>
Unix: $ export JBOSS_HOME=<where your jboss 4.0 distribution is> Windows: $ set JBOSS_HOME=<where your jboss 4.0 distribution is> $ ant
After building, you can then goto calculator.jsp