给你一个最简单的无状态的sessionBeanHelloWorldBean: */
package test;import javax.ejb.SessionBean;/**
* @ejb.bean name="HelloWorld"
* jndi-name="ejb/HelloWorldBean"
* view-type="remote"
*
*--
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
*
*--
**/public abstract class HelloWorldBean implements SessionBean {
/**
* @ejb.interface-method
* view-type="remote"
*
* @param echo
* @return
*/
public String echo(String echo)
{
return echo;
}
/**
* @ejb.create-method
*
*/
public void ejbCreate()
{
}
}Test.javapackage test;import java.rmi.RemoteException;
import java.util.Hashtable;import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;/**
* @author leqi
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestHelloWorld { private test.HelloWorldHome getHome() throws NamingException {
return (test.HelloWorldHome) getContext().lookup(
test.HelloWorldHome.JNDI_NAME);
} private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props);
return initialContext;
} public void testBean() { try {
test.HelloWorld myBean = getHome().create(); System.out.println(myBean.echo("hello world!")); } catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
TestHelloWorld test = new TestHelloWorld();
test.testBean(); }
}
package test;import javax.ejb.SessionBean;/**
* @ejb.bean name="HelloWorld"
* jndi-name="ejb/HelloWorldBean"
* view-type="remote"
*
*--
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
*
*--
**/public abstract class HelloWorldBean implements SessionBean {
/**
* @ejb.interface-method
* view-type="remote"
*
* @param echo
* @return
*/
public String echo(String echo)
{
return echo;
}
/**
* @ejb.create-method
*
*/
public void ejbCreate()
{
}
}Test.javapackage test;import java.rmi.RemoteException;
import java.util.Hashtable;import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;/**
* @author leqi
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestHelloWorld { private test.HelloWorldHome getHome() throws NamingException {
return (test.HelloWorldHome) getContext().lookup(
test.HelloWorldHome.JNDI_NAME);
} private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable(); props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); // This establishes the security for authorization/authentication
// props.put(InitialContext.SECURITY_PRINCIPAL,"username");
// props.put(InitialContext.SECURITY_CREDENTIALS,"password"); InitialContext initialContext = new InitialContext(props);
return initialContext;
} public void testBean() { try {
test.HelloWorld myBean = getHome().create(); System.out.println(myBean.echo("hello world!")); } catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
TestHelloWorld test = new TestHelloWorld();
test.testBean(); }
}
我建议你从网上找那些STEP BY STEP的例子,按例子一步步做。