tags:

views:

146

answers:

2

I have now

ear 
|----test.jar
     |-------META-INF
             |-------application.xml
     |-------test
             |-------Test.class
             |-------TestEJB.class
             |-------TestHome.class
|----test.war
     |-------WEB-INF
             |-------web.xml
     |-------test
             |-------TestServlet.class

application.xml

<application>
  <display-name>Ejb Test</display-name>
  <module>
    <ejb>test.jar</ejb>
  </module>
  <module>
    <web>
      <web-uri>test.war</web-uri>
      <context-root>/test</context-root>
    </web>
  </module>
</application>

web.xml

<web-app>
  <servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>test.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>

Is there something I'm missing from a working ear application and what's the purpose of these configuration files? How do I access the services of my ejb from the servlet?

+1  A: 

You'll need an EJB deployment descriptor in test.jar/META-INF/ejb-jar.xml and the EAR deployment descriptor has to go to your.ear/META-INF/application.xml (NOT inside the EJB jar!).

The ejb-jar.xml will contain something like the following XML fragment for your EJBs:

<enterprise-beans>
    <session>
        <ejb-name>MyCoolEJB</ejb-name>
        <local-home>test.TestHome</local-home>
        <local>test.Test</local>
        <ejb-class>test.TestEJB</ejb-class>
    </session>

    <!-- ... more <session/> or <entity/> entries ... -->
</enterprise-beans>

I worked with EJB3 myself, where things are a little bit different and you don't have the home interfaces any longer. So take my example with a grain of salt.

Inside the web.xml you'll have to have a corresponding reference to the EJB:

<ejb-local-ref>
    <ejb-ref-name>TheJNDINameOfYourEJB</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <ejb-link>MyCoolEJB</ejb-link>   <!-- the value from <ejb-name/> above -->
</ejb-local-ref>

The <ejb-link/> points to the corresponding <ejb-name/> of your EJB, if your reference is in the same JAR as your EJB. Otherwise, you'll have to preprend the name of the JAR such as <ejb-link>test.jar#MyCoolEJB</ejb-link>.

Then, in your servlet you can do a JNDI lookup for "java:comp/env/TheJNDINameOfYourEJB" to access the home interface of your EJB. I hope, that gave you a few pointers to delve deeper yourself.

BTW: If EJB 2.x is not a fixed requirement, then have a look at EJB 3.x. A lot of things got easier to use in that version!

janko
Is there also <ejb-ref> in addition to <ejb-local-ref> and what's the difference? Can't I just include the EJBHome part in my application and call the create directly without using refs and jndi?
JtR
The former is for references to the remote interface of an EJB, the latter for references to the local interface. See, e.g., http://www.jguru.com/faq/view.jsp?EID=1166568 for an explanation of the differences between all the interfaces.
janko
JNDI: In EJB 2.x you use the home interface's create() method to get a reference to your EJB. However, you need to have a reference to the home interface first and for this you need refs and JNDI.
janko
You can legally download the PDF of "Mastering EJB" which covers all of EJB 2.1 athttp://www.theserverside.com/tt/books/wiley/masteringEJB/
janko
+1  A: 

Your EJB doesn't seem well packaged: it should be in it's own EJB module and I can't see the EJB deployment descriptor (an ejb-jar.xml file). Have a look at Packaging Applications and Directory Structure in an EJB Module JAR File

Pascal Thivent