这里,我们编写一个基于控制台的application客户端来访问EJB:
// Client.java
import javax.naming.*;
import javax.rmi.*;
public class Client {
public static void main(String[] args) throws Exception {
// init context:
Context ctx = new InitialContext();
// 通过JNDI查找HelloBean:
Object obj = ctx.lookup("ejb/Hello");
// 获得home object:
HelloHome home = (HelloHome)PortableRemoteObject.narrow(obj, HelloHome.class);
// 创建ejb object:
Hello hello = home.create();
// 调用商业方法:
String Words = hello.say();
System.out.PRintln(words);
}
}
然后打开控制台,先设置好CLASSPATH,主要是将部署时生成的helloClient.jar添加到CLASSPATH中:
C:/client>set CLASSPATH=%CLASSPATH%;C:/client/helloClient.jar
然后编译:
C:/client>javac Client.java
运行:
C:/client>java Client
屏幕输出:
Hello, world.
我们的第一个EJB终于调用成功!
(出处:http://www.VeVb.com)
新闻热点
疑难解答