## EARs and WARs

Ears and wars are created just as they were in J2EE 1.4/EJB 2.1.

    <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>

#### Packaging Persistence Units

JBoss will not support the Java EE 5 EAR format until version JBoss 5.x. To deploy a persistence archive (a .jar with no EJBs and just persistence units), you must list it in application.xml as an ejb module.

#### Lookup of EJBs

@EJB annotations are usable in servlets and JSPs, but unfortunately, we have not yet updated tomcat to support it. Also, Tomcat works with the old XML format so you cannot use XML either. So for now, you must lookup the EJB via its global JNDI name. This is not compliant, but if you abstract out enough you'll be fine.

       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;
       }

#### Looking up EntityManagers from within an EAR/WAR

The @PersistenceUnit and <persistence-unit-ref> elements can be used within Servlets and JSPs to access EntityManagerFactory only. You cannot inject EntityManagers directly into web components. Unfortunately, again, Tomcat doesn't support them, so you have to lookup the EntityManagerFactory in JNDI. To be able to do that you need to set the `entity.manager.factory.jndi.name` property in your persistence.xml file:

    <persistence>
       <persistence-unit name="testdb">
          ...
       <properties>
          <property name="entity.manager.factory.jndi.name" value="java:/MyEntityManagerFactory"/>
       </properties>
    </persistence>

#### Building and Running

To build and run the example, make sure you have `ejb3.deployer` installed in JBoss 4.0.x and have JBoss running. See the reference manual on how to install EJB 3.0.

    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