<%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %> <%! //declare a "global" reference to an instance of the home interface of the session bean AccountHome accHome=null;
public void jspInit() { //obtain an instance of the home interface InitialContext cntxt = new InitialContext( ); Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); } %> <% //instantiate the session bean Account acct = accHome.create(); //invoke the remote methods acct.doWhatever(...); // etc etc... %> 在JSP中java代码应该越少越好。 在以上例子中,JSP设计者不得不处理和理解存取EJB的机理。 代替 在一个Mediator中对EJB机制的压缩以及将EJB方法作为Mediator的方法, 可以在jsp中使用 Mediator。Mediator通常由EJB设计者编写。Mediator可以提供附加的值如attribute caching等.
jsp与ejb通信 The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean:
<%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %> <%! //declare a "global" reference to an instance of the home interface of the session bean AccountHome accHome=null;
public void jspInit() { //obtain an instance of the home interface InitialContext cntxt = new InitialContext( ); Object ref= cntxt.lookup("java:comp/env/ejb/AccountEJB"); accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class); } %> <% //instantiate the session bean Account acct = accHome.create(); //invoke the remote methods acct.doWhatever(...); // etc etc... %> It is a good practise to use as little Java code as possible in a JSP as possible. In the above example, the JSP page designer has to deal with and understand the mechanics of accessing an EJB. Instead encapsule the EJB mechanics in a Mediator and expose the EJB methods as methods of the Mediator. Use the Mediator in the JSP. The Mediator is usually written by the EJB developer. The Mediator can provide additional value like attribute caching etc.
***** What is the most efficient approach for integrating EJB with JSP? Should the EJBs be invoked directly from within JSP scriptlets? Should the access take place from within Java beans? Or is it best to use custom tags for this purpose?
JSP scriptlet code should be minimal. Invoking EJB code directly on a JSP page results in many lines of code on your JSP page, including try...catch blocks to catch naming and finding exceptions.
Using a standard JavaBean as an intermediary between the JSP page and EJB server cuts down on the amount of code needed to add to a JSP page, and promotes reuse. The JavaBean should be a simple wrapper around the EJB you are accessing.
If you use a standard JavaBean you could also use the jsp:useBean tag to setup EJB parameters, such as the server URL and server security parameters.
Custom tags are also an option. However, they require a lot more coding than a simple JavaBean wrapper. The point should be to rewrite as little code as possible while at the same time keeping the JSP scriptlet content as light as possible.