• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > Jsp编程 >

jsp与ejb通信的实现方法

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-05-10点击:

我们用一个snippet代码,演示了JSP页面如何与 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...
%>
在JSP中java代码应该越少越好。
在以上例子中,JSP设计者不得不处理和理解存取EJB的机理。 代替 在一个Mediator中对EJB机制的压缩以及将EJB方法作为Mediator的方法,
可以在jsp中使用 Mediator。Mediator通常由EJB设计者编写。Mediator可以提供附加的值如attribute caching等.

*****

JSP scriptlet代码必须最小化。如果要在jsp中直接请求ejb可能要在jsp 中写许多代码,包括try...catch等函数块来进行操作。
使用一个标准的JavaBean作为一个jsp和EJB服务器的中介可以减少在jsp中的java代码的数量,并可提高可重用性。这个JavaBean必须是一个你所存取的EJB的覆盖(wrapper)。
如果你使用标准的JavaBean,你可以使用 jsp:useBean标记来初始化EJB参数,如服务器URL和服务器
安全参数等。
自定义标记也可以是一种选择。但是,这要求比一个简单JavaBean wrapper更多的编码。
该处必须被重新写为尽可能小的代码并保证JSP脚本内容尽可能轻(light)。
 提供原文如下:
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.

 

标签: jsp与ejb通信

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1170734538@qq.com ,  QQ