This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
hi 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.
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'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.
hi 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 view...so you implemented the the pure MVC design pattern..... please if there is better approach tell me ..