短裙黑色:GlassFish下使用EJB的简单例子
来源:百度文库 编辑:中财网 时间:2024/05/04 09:26:31
使用工具:MyEclipse9,GlassFish3
应用:单独的java程序访问远程EJB
一、Client端
1. create java project EJBClient; Add gf-client.jar (\glassfish3\glassfish\lib) to java build path
2. create client class FirstEjbTest:
public static void main(String[] args) { Context initial;
try {
System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.provider.url","http://localhost:8080");
initial = new InitialContext(); ASessionBeanHome home = (ASessionBeanHome)initial.lookup("jndi/ASessionBean");
ASessionBeanRemote remote = home.create();
remote.sayHello();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e){
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
二、EJB服务端
1. create EJB2.1 project EJB21Proj;
2. create session bean ASessionBean; public class ASessionBean implements SessionBean { /** The session context */
private SessionContext context; public ASessionBean() {
// TODO Auto-generated constructor stub
} public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub } public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub } public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub } /**
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
} /**
* An ejbCreate method as required by the EJB specification.
*
* The container calls the instance?s
* signature matches the signature of the
* by the client. The input parameters sent from the client are passed to
* the
* least one
* of a session bean?s
* session bean class.
*
* @throws CreateException Thrown if method fails due to system-level error.
*
* @ejb.create-method
*
*/
public void ejbCreate() throws CreateException {
// TODO Add ejbCreate method implementation
} /**
* An example business method
*
* @ejb.interface-method view-type = "both"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void replaceWithRealBusinessMethod() throws EJBException {
// rename and start putting your business logic here
}
public void sayHello(){
System.out.println("Hello EJB2.1!");
}
}
2. create home interface ASessionBeanhome; public interface ASessionBeanHome extends EJBHome {
public ASessionBeanRemote create() throws RemoteException,CreateException;
}
3. create remote interface ASessionBeanRemote;public interface ASessionBeanRemote extends EJBObject {
public void sayHello() throws RemoteException;
}
4. META-INF/ejb-jar.xml
EjbProj21
MyFirstSessionBean
ASessionBeanHome
ASessionBeanRemote
ASessionBean
Stateless
Bean
5. META-INF/sun-ejb-jar.xml(for GlassFish)
MyFirstSessionBean
jndi/ASessionBean
三、Deploy project Ejb21Proj to GlassFish server and start up the server
四、run FirstEjbTest.main()方法五、Console outputINFO: Hello EJB2.1!
应用:单独的java程序访问远程EJB
一、Client端
1. create java project EJBClient; Add gf-client.jar (\glassfish3\glassfish\lib) to java build path
2. create client class FirstEjbTest:
public static void main(String[] args) { Context initial;
try {
System.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.provider.url","http://localhost:8080");
initial = new InitialContext(); ASessionBeanHome home = (ASessionBeanHome)initial.lookup("jndi/ASessionBean");
ASessionBeanRemote remote = home.create();
remote.sayHello();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CreateException e){
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
二、EJB服务端
1. create EJB2.1 project EJB21Proj;
2. create session bean ASessionBean; public class ASessionBean implements SessionBean { /** The session context */
private SessionContext context; public ASessionBean() {
// TODO Auto-generated constructor stub
} public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub } public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub } public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub } /**
* Set the associated session context. The container calls this method
* after the instance creation.
*
* The enterprise bean instance should store the reference to the context
* object in an instance variable.
*
* This method is called with no transaction context.
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
} /**
* An ejbCreate method as required by the EJB specification.
*
* The container calls the instance?s
ejbCreate
method whose* signature matches the signature of the
create
method invoked* by the client. The input parameters sent from the client are passed to
* the
ejbCreate
method. Each session bean class must have at* least one
ejbCreate
method. The number and signatures* of a session bean?s
create
methods are specific to each * session bean class.
*
* @throws CreateException Thrown if method fails due to system-level error.
*
* @ejb.create-method
*
*/
public void ejbCreate() throws CreateException {
// TODO Add ejbCreate method implementation
} /**
* An example business method
*
* @ejb.interface-method view-type = "both"
*
* @throws EJBException Thrown if method fails due to system-level error.
*/
public void replaceWithRealBusinessMethod() throws EJBException {
// rename and start putting your business logic here
}
public void sayHello(){
System.out.println("Hello EJB2.1!");
}
}
2. create home interface ASessionBeanhome; public interface ASessionBeanHome extends EJBHome {
public ASessionBeanRemote create() throws RemoteException,CreateException;
}
3. create remote interface ASessionBeanRemote;public interface ASessionBeanRemote extends EJBObject {
public void sayHello() throws RemoteException;
}
4. META-INF/ejb-jar.xml
5. META-INF/sun-ejb-jar.xml(for GlassFish)
三、Deploy project Ejb21Proj to GlassFish server and start up the server
四、run FirstEjbTest.main()方法五、Console outputINFO: Hello EJB2.1!
请VB高手用浅显易懂的语言讲下该怎么使用Function,最好在举个简单的例子
wicepluse 的使用例子
EJB技术的理解
j2ee与ejb的介绍!
请举个简单的例子:C++作业!~
在JavaScript中单引号和双引号各在什么情况下使用?例如下面的例子:
现在使用C++的一般都是开发什么的?举下例子!~
<param>是在什么情况下用的??要如何用啊(请举简单的例子)
java编写简单例子
一个简单C++例子?
EJB的角色和三个对象
CORBA, DCOM, EJB 的组成和特点
EJB是基于哪些技术实现的?
请问什么是无状态的ejb?
三维的简单使用
哪里有比较简单的html程序编辑的例子?
只求一个最简单的验证例子(30分)
跪求:flash 与 asp 通讯的最简单例子
宏观调控是什么??能否举个简单的例子??
1. 什么叫数据结构? 试举一身边的简单例子说明。
vc能做什么 举个简单的例子
谁会做一个简单的FLASH例子给我
举简单生动的例子说明信托关系
请简单介绍两个生物给人们启示的例子