创建Web客户端
web客户端包含在examples/src/ejb/converter/index.jsp
的JSP网页中.JSP网页是一个包含静态模板数据基于文本的文档,它由HTML, WML, 和XML连同JSP元素一起组成,它们构成动态内容.
编写Web客户端
下面高亮显示的语句是用来定位home interface,创建enterprise bean实例,然后几乎和J2EE应用程序客户端一样的处理方式来调用商务方法.仅仅不同的只是lookup
方法的参数.
客户端需要的类在JSP的指令中导入(由<%@ %>
字符包含).因为定位home interace和创建enterprise bean只执行一次, 所以显示在JSP的声明当中(由字符<%! %>
包含), 它们包含JSP网页的初始化方法jspInit
.这项声明紧跟着的是创建只有一个输入框的表单的标准HTML标记.scriptlet(由字符<% %>
包含)从Request对象中取得一个参数然后把它转换成double型.最后, JSP(由字符<%= %>
包含)调用enterprise bean的商务方法且把结果加到数据流并返回到客户端.
<%@ page import="Converter,ConverterHome,javax.ejb.*,
javax.naming.*, javax.rmi.PortableRemoteObject,
java.rmi.RemoteException" %>
<%!
private Converter converter = null;
public void jspInit() {
try {
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("
java:comp/env/ejb/TheConverter");
ConverterHome home =
(ConverterHome)PortableRemoteObject.narrow(
objRef, ConverterHome.class);
converter = home.create();
} catch (RemoteException ex) {
...
}
}
...
%>
<html>
<head>
<title>Converter</title>
</head>
<body bgcolor="white">
<h1><center>Converter</center></h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get">
<input type="text" name="amount" size="25">
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<%
String amount = request.getParameter("amount");
if ( amount != null && amount.length() > 0 ) {
Double d = new Double (amount);
%>
<p><%= amount %> dollars are
<%= converter.dollarToYen(d.doubleValue()) %> Yen.
<p><%= amount %> Yen are
<%= converter.yenToEuro(d.doubleValue()) %> Euro.
<%
}
%>
</body>
</html>
编译Web客户端
J2EE server自动编译web客户端.
打包Web客户端
打包web组件,你需要运行deploytool
的New Web Component Wizard. 在处理期间,向导把客户端文件编译进一个WAR文件然后把这个WAR文件加到应用程序的ConverterApp.ear
文件中.
开始New Web Component Wizard,选择File->New Web Component. 向导显示下面的对话框.
- Introduction对话框:
- 阅读向导特性概览的说明文本.
- 单击 Next.
- WAR File对话框
- 在Application中选择Create New WAR File.
- 在组合框中,选择ConverterApp.
- 在WAR Display Name栏,输入
ConverterWAR
.
- 单击 Edit.
- 在Available Files目录树中,定位到
examples/build/ejb/converter
目录.
- 选择 index.jsp 然后单击 Add.
- 单击 OK.
- 单击 Next.
- Choose Component Type 对话框
- 选择JSP单选按钮.
- 单击 Next.
- Component General Properties 对话框
- 在JSP Filename组何框,选择 index.jsp.
- 在Web Component Name 栏,输入
converter
.
- 单击 Finish.
指定Web客户端的Enterprise Bean Reference
当调用lookup
方法时,web客户端refers to an enterprise bean:
Object objref = initial.lookup
("java:comp/env/ejb/TheConverter");
你应当如下指定reference:
- 在目录树中,选择 ConverterWAR.
- 选择EJB Ref's tab.
- 单击 Add.
- 在 Coded Name 列输入
ejb/TheConverter
.
- 在 Type 列,选择 Session.
- 在 Interfaces 列, 选择 Remote.
- 在 Home 列输入
ConverterHome
.
- 在 Local/Remote 列输入
Converter
.