三、创建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>
<%@ 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>
<!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>
<jboss-web>
<ejb-ref>
<ejb-ref-name>HaiEJB</ejb-ref-name>
<jndi-name>HaiEJB</jndi-name>
</ejb-ref>
</jboss-web>
