Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Look up code for EJB

 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
steve dowdall
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic