try {
Context initial=getContextInfo();
Object objref=initial.lookup("Menu");
MenuHome home=(MenuHome)javax.rmi.PortableRemoteObject.narrow(objref,MenuHome.class);
Menu menu=home.create();
menu.addDish("Tomato Soup");
menu.addDish("Sausage and Mash");
menu.addDish("Apple pie");
System.out.println(menu.checkStock());
} catch(Exception e) {
System.out.println(e);
}
Context initial=getContextInfo();
Object objref=initial.lookup("Menu");
MenuHome home=(MenuHome)javax.rmi.PortableRemoteObject.narrow(objref,MenuHome.class);
Menu menu=home.create();
menu.addDish("Tomato Soup");
menu.addDish("Sausage and Mash");
menu.addDish("Apple pie");
System.out.println(menu.checkStock());
} catch(Exception e) {
System.out.println(e);
}
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); //建立home interface供remotor interface调用
converter = home.create(); //调用home interface中的create方法,由ejb容器去调用ejbCreate()方法,返回一个远程接口
} 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 ) {
BigDecimal d = new BigDecimal (amount);
%>
<%//以下是调用remotor interface中的方法%>
<p><%= amount %> dollars are
<%= converter.dollarToYen(d) %> Yen.
<p><%= amount %> Yen are
<%= converter.yenToEuro(d) %> Euro.
<%
}
%>
</body>
</html>jsp调用ejb组件,只有你对j2ee应用工程部署后才能运行。