点击这里给我发消息 点击这里给我发消息

jsp与ejb通信的实现方法

添加时间:2013-12-6
    相关阅读: 设计 页面 JSP
 

我们用一个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.[Page]

*****
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.


 

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册