学习J2EE_(1)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

此书本来是用的BES(Borland的服务器)和WebLogic(bea的服务器),但这两个都比较庞大,所以我换成了JBOSS,相信大家也用的比较多。

EJB是J2EE服务器的核心组成部分,它可以提供事务、数据处理服务,EJB分为3个组成部分,分别是远程接口、创建接口吃事务处理,结构如图:

客户端程序的运行流程:

启动客户端的程序----通过JNDI名字找到服务端的EJB----创建HelloHome对象----创建Hello对象----访问sayHello方法

1、远程接口的代码如下:

/**

* 类 描 述:remote interface 用来揭示EJB对外的一些方法

* 类 名 称:

*

* 编 写 人:胡斐

* 编写日期:2004年07月30日

*/

package hellostandardserver;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

public interface Hello

extends EJBObject {

public String sayHello() throws RemoteException;

}

2、创建接口代码如下:

/**

* 类 描 述:创建接口

* 类 名 称:

*

* 编 写 人:胡斐

* 编写日期:2004年06月日

*/

package hellostandardserver;

import javax.ejb.EJBHome;

import javax.ejb.CreateException;

import java.rmi.RemoteException;

public interface HelloHome

extends EJBHome {

public Hello create() throws CreateException, RemoteException;

}

3、主文件的代码如下:

/**

* 类 描 述:bean implementation 是提供方法的实现,这些方法在上述两种interface中都有规定了

* 类 名 称:

*

* 编 写 人:胡斐

* 编写日期:2004年08月05日

*/

package hellostandardserver;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.ejb.CreateException;

public class HelloEJB implements SessionBean {

SessionContext sessionContext;

public String sayHello() {

return "你好,欢迎进入EJB的学习历程";

}

public HelloEJB() {}

//引入SessionBean接口必须声明的方法

public void ejbCreate() throws CreateException {

System.out.println("create 方法激活。");

}

public void ejbRemove() {

System.out.println("remove 方法激活。");

}

public void ejbActivate() {

System.out.println("active 方法激活。");

}

public void ejbPassivate() {

System.out.println("passivate 方法激活。");

}

public void setSessionContext(SessionContext sessionContext) {

this.sessionContext = sessionContext;

System.out.println("setsessioncontext 方法激活。");

}

}

4、客户端代码如下:

/**

* <p>Title: </p>

* <p>Description: </p>

* <p>Copyright: Copyright (c) 2004</p>

* <p>Company: </p>

* @author 胡斐

* @version 1.0

*/

import java.util.*;

import javax.naming.*;

import javax.rmi.*;

import hellostandardserver.*;

public class TestEJB {

public static void main(String[] args) {

try {

//jndi配置,硬编码到java中,应实现为外部属性文件

//对于不同服务器写法可能稍有不同

Properties p = new Properties();

p.setProperty("java.naming.factory.initial",

"org.jnp.interfaces.NamingContextFactory");

p.setProperty("java.naming.provider.url",

"localhost:1099");

//out print jndi 配置

p.list(System.out);

// Get a naming context,创建EJB的名字查找类

InitialContext jndiContext = new InitialContext(p);

System.out.println("Got context");

// Get a reference to the Interest Bean,通过JNDI名字找到Hello EJB

//jboss默认jndi 名为ejb-jar.xml 中的:ejb-name

Object ref = jndiContext.lookup("Hello");

System.out.println("Got reference");

// Get a reference from this to the Bean's Home interface

//通过PortableRemoteObject类的narrow方法取得HelloHome接口的应用

HelloHome home = (HelloHome)

PortableRemoteObject.narrow(ref, HelloHome.class);

// Create an Hello object from the Home interface

//通过HelloHome接口取得Hello接口的应用

Hello hello = home.create();

// call the hello() method

System.out.println(hello.sayHello());

}

catch (Exception e) {

System.out.println(e.toString());

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航