开发一个具有Remote和Local接口的EJB Stateless SessionBean
Remote接口RemoteHelloWorldjava
Local接口LocalHelloWorldjava
SessionBean:HelloWorldBeanjava
JNDI配置jndiproperites
JSPhellojsp
package comyourcompanyejb;
public interface RemoteHelloWorld{
public String Say(String name);
}
package comyourcompanyejb;
public interface LocalHelloWorld{
public String Say(String name);
}
package comyourcompanyejb;
import javaxejbStateless;
import javaxejbRemote;
import javaxejbLocal;
import comyourcompanyejbRemoteHelloWorld;
import comyourcompanyejbLocalHelloWorld;
@Remote({RemoteHelloWorldclass})
//注释表示RemoteHelloWorld为这个SessionBean的Remote接口
@Local({RemoteHelloWorldclass})
//注释表示LocalHelloWorld为这个SessionBean的Local接口
//@Stateless注释表示这是一个无状态会话Bean
public @Stateless class HelloWorldBean implements RemoteHelloWorldLocalHelloWorld{
public String Say(String name){
return 这是一个无状态的EJB会话BEAN作者+name;
}
}
//JNDI配置告诉了你的客户端初始化jndi naming service
jndiproperties:
javanamingfactoryinitial=orgjnpinterfacesNamingContextFactory
javanamingfactoryurlpkgs=orgjbossnaming:orgjnpinterfaces
javanamingproviderurl=localhost:
JSP中调用代码如下
<%
Properties prop=new Properties();
propload(ThreadcurrentThread()getContextClassLoader()getResourceAsStream (jndiproperties));
InitialContext ctx=new InitialContext(prop);
RemoteHelloWorld remotehelloworld=(RemoteHelloWorld)ctxlookup (HelloWorldBean/remote);
remotehelloworldSay(Christina[remote]);
LocalHelloWorld localhelloworld=(LocalHelloWorld)ctxlookup (HelloWorldBean/local);
localhelloworldSay(Christina[local]);
%>
运行结果
这是一个无状态的EJB会话BEAN作者Christina[remote]
这是一个无状态的EJB会话BEAN作者Christina[local]
总结步骤
先写好了SessionBean的业务逻辑接口
再写SessionBean记得在SessionBean实现了业务逻辑接口