在开始写代码之前请在你的磁盘上建立一个目录,这个目录中存放所有的程序的源代码和编绎后的代码和压缩包以及所有的xml配置文件。 在我这里是这样的,我在c盘的根目录下建立了一个目录叫做hdsite,同时在这个目录中建立一个目录叫做src的存放所有的源代码和工程中需要的配置文件。在src下建立一个java的目录存放所有的java源文件,建立一个xml目录,存放所有的xml配置文件。所建的目录具体如下:
hdsite/
hdsite/src/
hdsite/src/java/
hdsite/src/java.net/chinacode/hello/
hdsite/src/java.net/chinacode/hello/sejb/
hdsite/src/java.net/chinacode/hello/web/
hdsite/src/xml/
hdsite/src/xml/ejb/
hdsite/src/xml/web/
首先我们来完成home interface,因为home interface只是简单的完成了一个固定的工作,所以一个session bean的home interface差不多都一个样子:
package.net.chinacode.hello;
import java.rmi.*;
import javax.ejb.*;
public interface hellohome extends ejbhome
{
public hello create() throws createexception, remoteexception;
}
我们将它存为hdsite/src/java.net/chinacode/hello中的hellohome.java。它继承了ejbhome的create方法,它返回hello这个ejb remote interface。哪么remote interface怎么写呢?我们首先需要确定我们的ejb要完成客户的什么样的功能。我们希望向ejb输入一个人名,由ejb返回hello 人名。调用ejb的方法名就叫做sayhello,哪么这个方法就应体现到remote interface中去,代码如下:
package.net.chinacode.hello;
import java.rmi.*;
import javax.ejb.*;
public interface hello extends ejbobject
{
public string sayhello(string strname) throws remoteexception;
}
我们将它存为hdsite/src/java.net/chinacode/hello中的hello.java。它只是说明了在ejb中会向用户提供的方法。哪么具体的实现则由ejb中的bean实体来完成,其代码如下:
package.net.chinacode.hello;
import java.rmi.*;
import javax.ejb.*;
public class hellobean extends object implements sessionbean{
public string sayhello(string strname) {
return "hello " + strname + "...";
}
public void ejbcreate() throws ejbexception, remoteexception {
}
public void ejbactivate() throws ejbexception, remoteexception {
}
public void ejbpassivate() throws ejbexception, remoteexception {
}
public void ejbremove() throws ejbexception, remoteexception {
}
public void setsessioncontext(sessioncontext context) throws ejbexception, remoteexception {
}
}
我们将这个文件存为hdsite/src/java.net/chinacode/hello/hellobean.java。这里除了sayhello方法之外都是继承sessionbean类来实现的方法,一般情况下都是固定的。如果你需要在服务器对ejb进行相关操作时要在bean中进行一些操作,哪么你可以在相应的ejbcreate、ejbremove这样的方法中写入自己的代码。好了,这三个就是我们的ejb的三个组成部分了,我们还需要给orion一个xml文件,在这个xml文件中说明我们要使用的ejb的框架。这个xml文件的内容如下:
<?xml version="1.0"?>
<ejb-jar>
<display-name>hd test ejb (ejb module)</display-name>
<description>hd ejb sampale</description>
<enterprise-beans>
<session>
<display-name>the helloejb session bean</display-name>
<ejb-name.net.chinacode.hello.hello</ejb-name>
<home.net.chinacode.hello.hellohome</home>
<remote.net.chinacode.hello.hello</remote>
<ejb-class.net.chinacode.hello.hellobean</ejb-class>
<session-type>stateless</session-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<security-role>
<description>users</description>
<role-name>users</role-name>
</security-role>
</assembly-descriptor>
</ejb-jar>
这个xml文件说明.net.chinacode.hello.hello这个ejb的框架。我来看看在enterprise-beans段中的一个session段说明了一个session bean的结构,说明了这个ejb的home internface、bean class、remote interface,同时也向orion说明这个session bean的类型是stateless。我们把这个文件存入hdsite/src/xml/ejb/ejb-jar.xml文件中。这样我们就完成了一个系统中的ejb这部分。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器