程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> JBoss下的EJB3開發無狀態會話Bean

JBoss下的EJB3開發無狀態會話Bean

編輯:J2EE

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實現了業務邏輯接口


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved