File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes How to use EJB with Struts???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to use EJB with Struts????" Watch "How to use EJB with Struts????" New topic

How to use EJB with Struts????

Pras Tiwari
Ranch Hand

Joined: Nov 07, 2005
Posts: 186

I have created Successfully EJB Beans for my Application. Now for my web application I want to insert record into database. Where should I write lookup code for bean? Currently I am writing it in Action file inside execute method. Is it right way to do that? If not then waht is the best way to use EJB's with struts?

Please tell me.

Thanx in advance.


********Deserve Before You Desire********
poornima balagopal
Ranch Hand

Joined: Dec 02, 2003
Posts: 83
i am not sure about this approach. What i was doing in one of my project is i use a Helper class which in turn have the ejb lookup part and other implementation part. So only this class call used be there in my action class.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I worked on one major Struts/EJB project where we did that...performed the JNDI lookup in the action code. It works. We used a utility class where you passed in the name of the ejb home. The downside to this approach is that your "web" tier is now dealing with EJB specific tasks and exceptions (as long as you count your Action classes to be part of your web tier).

The project that I have been on for the past few years uses a "Business Delegate" pattern to isolate the web tier from the business tier. This delegate primarily handles JNDI lookup and exception translation (translating EJB specific exceptions into generic project exceptions). After coding dozens of these delegates by hand (they are pretty boring...they mostly just pass through parameters) one of the developers rolled out a dynamic delegate. It uses reflection and an interface that your EJB implements. It introduces some runtime overhead, but saves us from having to hand code delegate code. I am pretty sure his code was based of something pulled off the Internet.

I am sure if you search the web for "Business Delegate" or "Dynamic Delegate" you will find more info.

- Brent
N Nanda
Ranch Hand

Joined: Feb 09, 2004
Posts: 62

Brent's approach is absolutely fine. Use a Business Deligate class which is nothing but a plane java class (POJO approach) which takes care of all EJB specific tasks. However, don't put your JNDI lookups into deligate class. Rather use Service Locator pattern to do that. A Service Locator is again a java class which may contain methods (say) like , which may do all the JNDI lookups for an EJB (say) CustomerManagerEJB and keeps that remote reference in a Hashtable so that next time you require the same reference, you can get it from the Hashtable. Just search in SUN's web site for Service Locator pattern. And with the help of Business Deligate and Service Locator patterns you can isolate your business layer from web layer. And your struts action class or other servlets may call methods on Business Deligate class only.

Hope this helps.

Alan Hermin
Ranch Hand

Joined: Feb 16, 2006
Posts: 290
i think the best approach is to use the struts as controller ,just. and if you deal with the database, use JDO or DAO in the execute method to call the business (ejb) which in turn call the database...
to make the best approach use the javaserverfaces as you implemented the the pure MVC design pattern.....
please if there is better approach tell me ..
I agree. Here's the link:
subject: How to use EJB with Struts????
It's not a secret anymore!