EJB:
目录:
Remote: hello.java
home: hellHome.java
EjbBean:helloBean.java
业务实现:dao.java
客户端:
package gg.ejb;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.spi.NamingManager;
import javax.rmi.PortableRemoteObject;
import yourcompany.com.hello;
import yourcompany.com.helloHome;
public class testH {
/**
* @param args
*/
public static void main(String[] args) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:11099");//11099与server.xml文件中的配置相符。
env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
try{
Context ctx = new InitialContext(env);
Object obj = ctx.lookup( "ejb/hello" );
helloHome home =(helloHome)javax.rmi.PortableRemoteObject.narrow(
obj, helloHome.class );
hello helloWorld = home.create();
System.out.println( helloWorld.helloworld());
helloWorld.remove();
}catch(Exception ex){
System.out.println("error: "+ex);
}
}
}