Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2416
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2416
95
Android Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good point. Thanks Frits
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic