my dog learned polymorphism*
The moose likes Servlets and the fly likes Look up code for EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Look up code for EJB" Watch "Look up code for EJB" New topic
Author

Look up code for EJB

steve dowdall
Ranch Hand

Joined: May 23, 2001
Posts: 36
Hi,
I have a servlet that will call an ejb. I need to find two things:
1. the code to put into the servlet to do the lookup for the ejb.
2. should I put that lookup code into the init() of the servlet and then call my business methods from another method in the servlet?
Thanks,
Steve
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Well, EJB references are not guaranteed to be threadsafe, so you should probably look up your EJB reference everytime you do a doPost() or a doGet() method, rather than in the init() method. Just do the lookup then call the business methods one right after the other.
(Hopefully you're talking about using a stateless session bean, right? If not, we've got more to talk about )
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
steve dowdall
Ranch Hand

Joined: May 23, 2001
Posts: 36
Thanks Kyle,
Yup, SSB is what we are talking about. Per your suggestion I'll put it in a service method instead of the init(). Do you have a snippet of code perhaps that I would use to do the ejb lookup from this method?
Steve
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Look around page 54 of the EJB 1.1 specification. Reading that chapter should fill you in pretty well...
It will basically go:
// first set up a properties to correctly get an initial context
//(the details are dependent on your app server)
InitialContext initContext = new InitialContext(properties);
Object ref = initContext.lookup("someJndiName");
MyHome home = (MyHome) PortableRemoteObject.narrow(ref, MyHome.class);
MyEJB ejb = MyHome.create();

Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
steve dowdall
Ranch Hand

Joined: May 23, 2001
Posts: 36
Thanks a bunch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Look up code for EJB
 
Similar Threads
Changes made in JNDI by servlet not seen in EJB
how 2 call ejb file& servlet ie servlet& ejb communication
Why does it die after init()?
Is there any way to not load balance EJB call in WAS cluster?
communicate one ejb bean to another ejb bean