jQuery in Action, 2nd edition*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes WEB-INF/ejb-jar.xml within WAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "WEB-INF/ejb-jar.xml within WAR file" Watch "WEB-INF/ejb-jar.xml within WAR file" New topic
Author

WEB-INF/ejb-jar.xml within WAR file

Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi,
It just occurred to my mind if we have a web app structure like below, is that valid? I guess not, if not please explain how.



Appreciate if somebody can help me out.


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1634
    
  23

Hi Naren,

That structure is perfectly fine. Just try the following example:

Create a EJB project in Eclipse (or another IDE) and call in LoggerEJB and create the following EJB
and
the ejb-jar.xml (in META-INF) will be empty like this:

Export this project (right-click on the project) to an EJB JAR file and call it LoggerEJB.jar (just put it in a temp directory or on your desktop for the mean time)

Create a Dynamic Web Project in Eclipse and call it WebLogger, and add the following files:
and a ejb-jar.xml in the WEB-INF directory like this:

Move your exported EJB JAR file (LoggerEJB.jar) into WEB-INF/lib.

Create a Servlet called WebLoggerServlet into the nl.log.servlet package

and a web.xml like this:

Start your server and verify the JNDI names:
INFO: EJB5181:Portable JNDI names for EJB EchoBean: [java:global/WebLogger/EchoBean, java:global/WebLogger/EchoBean!nl.log.ejb.EchoBean]
INFO: EJB5181:Portable JNDI names for EJB LogBean: [java:global/WebLogger/LogBean, java:global/WebLogger/LogBean!nl.notes.ejb.Log]


Hit the following URL
http://localhost:8080/WebLogger/WebLoggerServlet

and verify on your web-page:
ECHO: echo from the WebLoggerServlet
and on your console:
INFO: logBean


Regards,
Frits
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Thanks Frits. I will try it out.

The reason I was thinking the other way was if I override or define EJB in both the ejb-jar.xmls (assume no annotations used), would it still work? Are rules similar to annotations with ejb-jar.xml within a <ejb jar>.jar(in this case may be WEB-INF/ejb-jar.xml overrides the internal ejb-jar.xml)? I'll try this out.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1634
    
  23

It won't work if you define the same EJB in both ejb-jar.xml's because they share the same namespace. You will get a name clash (javax.naming.NameAlreadyBoundException).

Important to remember is that an EJB in WEB-INF/classes is logically the same as an EJB (in a jar-file) in WEB-INF/lib.

Regards,
Frits
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Very good point. Thanks Frits
 
 
subject: WEB-INF/ejb-jar.xml within WAR file
 
Similar Threads
EJB3 - NullPointerException with @EJB Injection
linking webapp with ejb in ear
Expose EJBs within .war to other modules within the same EAR
Global JNDI access with EJB 3.1 and JBoss 6
deployment of WAR file from ANT fails