wood burning stoves*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Calling an EJB from within a Servlet - problem with lookup() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Calling an EJB from within a Servlet - problem with lookup()" Watch "Calling an EJB from within a Servlet - problem with lookup()" New topic
Author

Calling an EJB from within a Servlet - problem with lookup()

James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
Just for grins I am trying to call an EJB's business method from within a Tomcat web application. I have tried to call a simple EJB, the AdviceBean from the AdviceApp (first tutorial of "Head First EJB"), using the same code from the AdviceApp client, from within the servlet's doPost() method. I am getting a NameNotFoundException, complaining that the "Name Advisor is not bound in this Context". The J2EE RI server is running, the EJB is deployed, and the client jar file with all of the EJB classes and interfaces (created by RI container at deploy time) is included in the WEB-INF/lib directory of the webapp.
The servlet code is below, maybe someone can see what it is I'm doing wrong or leaving out. Basically the servlet's job is to fill a JavaBean with the advice string from the EJB's getAdvice() method, then add the bean to the session, to be displayed on a following JSP using a <jsp:getProperty> tag. Also included after the servlet code is the Tomcat web application's web.xml, which has an <ejb-ref> for the bean, but this doesn't seem to be enough to get my EJB bound into the context (I guessed at what the correct <ejb-ref> element values should be).
Any comments or suggestions will be quite appreciated.

And the XML:

[ December 07, 2003: Message edited by: Michael Ernest ]
James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
Sorry that the above code is so ugly - I guess this forum system doesn't preserve indentation. Just for future reference, is there a way to have code indentation preserved in postings ?
Thanks.

-James
Sunil Patil
Greenhorn

Joined: Feb 05, 2003
Posts: 15
Hi James,
As far as i know Tomcat is Servlet Container and it does not have a EJB Container.So i guess your deploying your EJB in Reference implementation or some other EJB server and trying to access it from application deployed in Tomcat.
In that case Tomcat will have its own Context which will be different from EJB Containers Context and when you do Context initContext = new InitialContext(); in Tomcat you will get Tomcat's context which will be different from EJB Containers Context where name Advice is registered for AdviceBean.
I had tried deploying both Servlet and EJB in Reference implementation and it works fine.
Sunil
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

James - you can use the UBB CODE tag to preserve indentation, as I did above.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
James Adams
Ranch Hand

Joined: Sep 12, 2003
Posts: 188
Thanks for the reply to my posting and for the tip on getting code formatting preserved on this forum.
Yes it makes sense that Tomcat and the RI both have their own JNDI contexts. I am confused however by the fact that the client code (which is run at the command line) should also have its own context separate from the context of the RI, but it runs fine without doing anything special (it just gets a context with "new InitialContext();") and it is then able to do the lookup with no problem. I assumed that my servlet running in Tomcat would be able to do the same thing but this is obviously wrong by virtue of the Tomcat server having its own context, and the vanilla call to construct an InitialContext returns Tomcat's context instead of the RI context which is what I need. Does the client code just get the RI's context by default since it's not running in some sort of container with a context ?
Perhaps this is getting into JNDI details which are tangential to my objective of learning EJBs. However it's interesting to find out how all of this works, so if there's a simple explanation or solution I'd appreciate the chance to learn more about this (i.e. please comment!). Also it never occured to me that the RI is a J2EE server so I can deploy and run web applications there as well, no need to use Tomcat and the RI in tandem. I guess I should learn how to do that now (hopefully it's as easy as deploying on Tomcat).
Thanks in advance for any further feedback/insight.

-James
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If you look into the j2ee.jar file, you'll see the same files you get when you download Tomcat, eg in the org.apache.catalina package. I suspect that RI is basically an EJB container which seamlessly runs Tomcat to provide servlet container services.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling an EJB from within a Servlet - problem with lookup()