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 ]
Joined: Sep 12, 2003
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.
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
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
Joined: Sep 12, 2003
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.
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
subject: Calling an EJB from within a Servlet - problem with lookup()