| |
jboss 4.0自动集成了tomcat 5.0,因而它不仅仅是专业的ejb容器,同时也成了专业的jsp/servlet容器和web服务器。 tomcat 5.0 被集成在 jboss的下面这个目录中:jboss_home/server/default/deploy/jbossweb-tomcat50.sar 里面提供了一个server.xml和web.xml文件,可以对tomcat进行一些基本设置。但jboss的设计者建议用户甚至不要接触这个目录,更不用说修改和添加什么文件了,因为tomcat与jboss是如此紧密相连,以致于所有的设置工作都可以在jboss自己的配置文件中完成。但是我还是发现了一个地方值得修改:修改server.xml中的端口号,比如由缺省的8080端口改为4000端口,因为我发现我的“百度下吧”软件常常占用了8080端口,而且我本来已经有了一个tomcat 5.0。 这样键入:http://localhost:4000/可以访问到jboss的欢迎页面。 本文演示了jsp页面如何在jboss 4.0中调用ejb组件。 总结构: 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!
|
|