总结构:
J2EE应用/
|__EJB组件/(haiejb.jar)
| |__META-INF/
| | |__ejb-jar.xml
| | |__jboss.xml
| |__ejbs/
| |__HaiHome.class
| |__HaiClient.class
| |__HaiBean.class
|__WEB应用/(haiejb.war)
| |__haiejb.jsp
| |__WEB-INF/
| |__web.xml
| |__jboss-web.xml
|__META-INF/
|__application.xml
一、编译java文件为EJB类文件
java文件编译:
[假定在系统环境变量的CLASSPATH中包含了javax.ejb.*包,该包可以在以下地方找:
JBOSS_HOME\server\default\lib\jboss-j2ee.jar
JBOSS_HOME\client\jboss-j2ee.jar]
[java源文件目录]>:javac -classpath %classpath% -d [输出目录:EJB组件目录] *.java
HaiHome.java:
package ejbs;
import java.io.Serializable;
import java.rmi.*;
import javax.ejb.*;
public interface HaiHome extends EJBHome {
HaiClient create() throws RemoteException, CreateException;
}
HaiClient.java:
package ejbs;
import javax.ejb.*;
import java.rmi.RemoteException;
public interface HaiClient extends EJBObject {
public String sayHai() throws RemoteException;
}
haiBean.java:
package ejbs;
import javax.ejb.*;
import javax.naming.*;
public class HaiBean implements SessionBean {
public String sayHai() {
return "Hai, EJB technology!";
}
public void ejbCreate() throws EJBException {}
public void ejbRemove() throws EJBException {}
public void ejbPassivate() {}
public void ejbActivate() {}
public void setSessionContext(SessionContext sc) {}
}
二、创建EJB组件:
haiejb.jar:(EJB组件)
打包命令:[EJB组件目录]>:jar cvf haiejb.jar META-INF/ ejbs/
|__META-INF/
| |__ejb-jar.xml
| |__jboss.xml
|__ejbs/
|__HaiHome.class
|__HaiClient.class
|__HaiBean.class
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<description>Hai EJB instance.</description>
<display-name>Hai EJB</display-name>
<enterprise-beans>
<session>
<ejb-name>HaiEJB</ejb-name>
<home>ejbs.HaiHome</home>
<remote>ejbs.HaiClient</remote>
<ejb-class>ejbs.HaiBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
jboss.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>HaiEJB</ejb-name>
<jndi-name>HaiEJB</jndi-name>
</session>
</enterprise-beans>
</jboss>
三、创建WEB应用
haiejb.war:(WEB应用)
打包命令:[WEB应用目录]>:jar cvf haiejb.war haiejb.jsp WEB-INF/
|__haiejb.jsp
|__WEB-INF/
|__web.xml
|__jboss-web.xml
haiejb.jsp:
<%@ page contentType="text/html;charset=GBK" %>
<%@ page import="ejbs.*,javax.ejb.*,javax.naming.*,javax.rmi.PortableRemoteObject,java.rmi.RemoteException" %>
<html>
<body>
<%
String message = "nothing!";
try {
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("HaiEJB");
HaiHome home = (HaiHome) PortableRemoteObject.narrow(objRef,ejbs.HaiHome.class);
HaiClient haiRemote = home.create();
message = haiRemote.sayHai();
} catch (RemoteException re) {
re.printStackTrace();
} catch (CreateException ce) {
ce.printStackTrace();
} catch (NamingException ne) {
ne.printStackTrace();
}
%>
<h1><%=message%></h1>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<ejb-ref>
<ejb-ref-name>HaiEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>ejbs.HaiHome</home>
<remote>ejbs.HaiClient</remote>
</ejb-ref>
</web-app>
jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<ejb-ref>
<ejb-ref-name>HaiEJB</ejb-ref-name>
<jndi-name>HaiEJB</jndi-name>
</ejb-ref>
</jboss-web>
四、创建J2EE应用程序
haiejb.ear: (J2EE应用程序)
把上面创建的haiejb.jar和haiejb.war包拷贝到你创建的J2EE应用主目录,新建一个META-INF目录并在里面创建application.xml文件:
打包命令:[J2EE应用目录]>:jar cvf haiejb.ear haiejb.jar haiejb.war META-INF/
|__haiejb.jar
|__haiejb.war
|__META-INF/
|__application.xml
application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<display-name>HaiEJB J2EE Application</display-name>
<module>
<web>
<web-uri>haiejb.war</web-uri>
<context-root>/haiejb</context-root>
</web>
</module>
<module>
<ejb>haiejb.jar</ejb>
</module>
</application>
五、部署J2EE应用:
把haiejb.ear拷贝到
JBOSS_HOME\server\default\deploy\
启动jboss 4.0,注意命令行窗口中有无异常,如果有异常情况,请查看log文件:
JBOSS_HOME\server\default\log\server.log
从中查找分析问题所在,然后改正之,直到无异常显示
最后在浏览器地址栏中键入:
http://localhost:8080/haiejb/haiejb.jsp
结果:
Hai, EJB technology!