Below is a basic example of a Servlet, who uses the
DynamicDelegateFactory to
dynamically create a business delegate for the HelloBean. Note that the
HelloBean implements the
Business Interface
design pattern by implementing the Hello interface.
// A servlet that uses the Dynamic Delegate to call the below bean.
public class MyServlet extends HttpServlet {
public MyServlet() { super(); }
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
DynamicDelegateFactory delegateFactory = new DynamicDelegateFactory();
Hello delegate = (Hello) delegateFactory.createLocalDelegate(
"java:comp/env/ejb/HelloLocalHome",
HelloLocalHome.class,
Hello.class);
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println(delegate.sayHello());
pw.close();
}
}
// The Business interface
public interface Hello {
String sayHello();
}
// The local client interface, implements the business interface (Hello)
public HelloLocal extends EJBLocalObject, Hello {
}
// The local home
public HelloLocalHome implements EJBLocalHome {
public HelloLocal create() throws CreateException;
}
// The bean class, implements the business interface (Hello)
public HelloBean implements SessionBean, Hello {
public String sayHello() { return "Hello"; }
public void ejbCreate() { }
public void ejbActivate() { }
public void ejbPassivate() { }
public void ejbRemove() { }
public void setSessionContext(SessionContext ctx) { }
}
You can also browse the source code of the
DynamicDelegateFactoryTest
test case.