1。開發一個具有Remote和Local接口的EJB3 Stateless SessionBean.
如有那裡寫的不好請大家多多指教。
##############################
## 好了先介紹一下明星演員們:#
##############################
Remote接口:RemoteHelloWorld.Java
Local接口:LocalHelloWorld.Java
SessionBean:HelloWorldBean.Java
JNDI配置:jndi.properites
JSP:hello.JSP package com.yourcompany.ejb3;
public interface RemoteHelloWorld{
public String Say(String name);
}
package com.yourcompany.ejb3;
public interface LocalHelloWorld{
public String Say(String name);
}
package com.yourcompany.ejb3;
import Javax.ejb.Stateless;
import Javax.ejb.Remote;
import Javax.ejb.Local;
import com.yourcompany.ejb3.RemoteHelloWorld;
import com.yourcompany.ejb3.LocalHelloWorld;
@Remote({RemoteHelloWorld.class})
//注釋表示RemoteHelloWorld為這個SessionBean的Remote接口
@Local({RemoteHelloWorld.class})
//注釋表示LocalHelloWorld為這個SessionBean的Local接口
//@Stateless注釋表示這是一個無狀態會話Bean
public @Stateless class HelloWorldBean implements RemoteHelloWorld,LocalHelloWorld{
public String Say(String name){
return "這是一個無狀態的EJB3會話BEAN,作者:"+name;
}
}
//JNDI配置告訴了你的客戶端初始化jndi naming service
jndi.propertIEs:
Java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
Java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
Java.naming.provider.url=localhost:1099
JSP中調用代碼如下:
<%
Properties prop=new PropertIEs();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jndi.propertIEs"));
InitialContext ctx=new InitialContext(prop);
RemoteHelloWorld remotehelloworld=(RemoteHelloWorld)ctx.lookup("HelloWorldBean/remote");
remotehelloworld.Say("Christina007[remote]");
LocalHelloWorld localhelloworld=(LocalHelloWorld)ctx.lookup("HelloWorldBean/local");
localhelloworld.Say("Christina007[local]");
%>
運行結果:
這是一個無狀態的EJB3會話BEAN,作者:Christina007[remote]
這是一個無狀態的EJB3會話BEAN,作者:Christina007[local]
總結步驟:
1.先寫好了SessionBean的業務邏輯接口
2.再寫SessionBean,記得在SessionBean實現了業務邏輯接口