• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James - you can use the UBB CODE tag to preserve indentation, as I did above.
 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic