wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Best Practice For Accessing EJB's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Best Practice For Accessing EJB Watch "Best Practice For Accessing EJB New topic
Author

Best Practice For Accessing EJB's

Jon Kidd
Greenhorn

Joined: Oct 11, 2002
Posts: 29
Hi all,
Just still finding my way around ejb's etc so forgive me if this is no brainer to you...
I've created a stateful session ejb ok and can access it's business methods succesfully using JNDI lookup code within a JSP page.
However, my question is, if I wanted to access that same session bean on other JSP pages do I need to replicate the JNDI lookup code to all the other pages or is there any easier way (I was thinking of custom tags?) ?
Thanks in advance.
Jon
Alex Ioannou
Greenhorn

Joined: Nov 14, 2001
Posts: 25
Short reply :-
Write a helper class that does all the JNDI so that in jsp you simply call something like :-
EJB = EJBHelper.getEJBHome().create()
or whatever method you wish to use. I always write a helper class for each EJB as a rule of thumb.
Ram
Ranch Hand

Joined: Apr 07, 2003
Posts: 43
Check out Service Locator pattern
Jon Kidd
Greenhorn

Joined: Oct 11, 2002
Posts: 29
Thanks for the replies to this, much appreciated.
Alex - So to get this straight in my head, you code a new helper class for each EJB that you create ?
Any chance you could post a smaple piece of code ?
Thanks
Jon
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Jon Kidd:
Thanks for the replies to this, much appreciated.
Alex - So to get this straight in my head, you code a new helper class for each EJB that you create ?
Jon

No. you have a single class with only one instance (singleton) and all your ejb's use it. it's all you need.
code... ? there's plenty
http://java.sun.com/blueprints/code/jps131/src/com/sun/j2ee/blueprints/servicelocator/web/ServiceLocator.java.html
http://java.sun.com/blueprints/patterns/ServiceLocator.html
http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html
and an improvement to service locator pattern that deals with cache issues:
http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-service.html
also, for more info:
EJB design patterns
in this book it is called EJBHomeFactory.
cheers


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Jon Kidd
Greenhorn

Joined: Oct 11, 2002
Posts: 29
Sheesh, and all that is supposed to make it simpler is it ?
I'll look into it further though and give it a shot.
Thanks,
Jon
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
ServiceLocator is a home factory & cache. I did done something similar a few years ago. It's up and running, but I always worried about it because it does not detect stale or bad homes. Is this a risk if you cache a home for a remote server and the remote server is stopped and restarted? Your cached homes are no good, but there's nothing to tell you to get fresh ones. Or have I just not read ServiceLocator closely enough?
Edit: D'oh! There was the "verified service locator" among your links!
[ April 28, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Stan James:
ServiceLocator is a home factory & cache. I did done something similar a few years ago. It's up and running, but I always worried about it because it does not detect stale or bad homes. Is this a risk if you cache a home for a remote server and the remote server is stopped and restarted? Your cached homes are no good, but there's nothing to tell you to get fresh ones. Or have I just not read ServiceLocator closely enough?
Edit: D'oh! There was the "verified service locator" among your links!
[ April 28, 2003: Message edited by: Stan James ]

that's exactly what the article with the link I provided is trying to solve.
http://www.javaworld.com/javaworld/jw-07-2002/jw-0703-service.html

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best Practice For Accessing EJB's
 
Similar Threads
access one EAR from another EAR
simplest way to restrict access to remote EJB calls
Web Services for EJB Session Beans
Reference To Stateful Session Beans
EJB 3.0, Application-client vs. Web-client