﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Cool eye-随笔分类-EJB</title><link>http://www.blogjava.net/joeyeezhang/category/7272.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:46:27 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:46:27 GMT</pubDate><ttl>60</ttl><item><title>A easy ejb sample</title><link>http://www.blogjava.net/joeyeezhang/archive/2006/02/06/29691.html</link><dc:creator>joeyeezhang</dc:creator><author>joeyeezhang</author><pubDate>Mon, 06 Feb 2006 05:19:00 GMT</pubDate><guid>http://www.blogjava.net/joeyeezhang/archive/2006/02/06/29691.html</guid><wfw:comment>http://www.blogjava.net/joeyeezhang/comments/29691.html</wfw:comment><comments>http://www.blogjava.net/joeyeezhang/archive/2006/02/06/29691.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/joeyeezhang/comments/commentRss/29691.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/joeyeezhang/services/trackbacks/29691.html</trackback:ping><description><![CDATA[<DIV id=content3><STRONG><FONT size=2></FONT></STRONG>&nbsp;</DIV>
<DIV id=newscontent>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P>
<P><STRONG><FONT size=2>总结构：</FONT></STRONG></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2>J2EE应用/<BR></FONT><FONT size=2>|__EJB组件/(haiejb.jar)<BR></FONT><FONT size=2><FONT size=2>|&nbsp; |__META-INF/<BR></FONT><FONT size=2>|&nbsp; |&nbsp; |__ejb-jar.xml<BR></FONT><FONT size=2>|&nbsp; |&nbsp; |__jboss.xml<BR></FONT><FONT size=2>|&nbsp; |__ejbs/<BR></FONT><FONT size=2>|&nbsp;&nbsp;&nbsp;&nbsp; |__HaiHome.class<BR></FONT><FONT size=2>|&nbsp;&nbsp;&nbsp;&nbsp; |__HaiClient.class<BR></FONT><FONT size=2>|&nbsp;&nbsp;&nbsp;&nbsp; |__HaiBean.class<BR></FONT>|__WEB应用/(haiejb.war)<BR><FONT size=2>|&nbsp; |__haiejb.jsp<BR></FONT><FONT size=2>|&nbsp; |__WEB-INF/<BR></FONT><FONT size=2>|&nbsp;&nbsp;&nbsp;&nbsp; |__web.xml<BR></FONT><FONT size=2>|&nbsp;&nbsp;&nbsp;&nbsp; |__jboss-web.xml<BR></FONT><FONT size=2>|__META-INF/</FONT>&nbsp;<BR><FONT size=2>&nbsp;&nbsp; |__application.xml</FONT></FONT></P>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P>
<P><STRONG><FONT size=2>一、编译java文件为EJB类文件</FONT></STRONG></P>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P><FONT size=2>
<P><FONT size=2><STRONG>java文件编译：</STRONG></FONT></P>
<P><FONT size=2>[假定在系统环境变量的CLASSPATH中包含了javax.ejb.*包，该包可以在以下地方找：</FONT></P>
<P><FONT size=2>JBOSS_HOME\server\default\lib\jboss-j2ee.jar</FONT></P>
<P><FONT size=2>JBOSS_HOME\client\jboss-j2ee.jar]</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><STRONG>[java源文件目录]&gt;:javac -classpath %classpath% -d [输出目录：EJB组件目录] *.java</STRONG></FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>HaiHome.java:</U></FONT></P>
<P><FONT size=2>package ejbs;</FONT></P>
<P><FONT size=2>import java.io.Serializable;<BR>import java.rmi.*;<BR>import javax.ejb.*;</FONT></P>
<P><FONT size=2>public interface HaiHome extends EJBHome {<BR>&nbsp;&nbsp;&nbsp; HaiClient create() throws RemoteException, CreateException;&nbsp;&nbsp; <BR>}</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>HaiClient.java:</U></FONT></P>
<P><FONT size=2>package ejbs;</FONT></P>
<P><FONT size=2>import javax.ejb.*;<BR>import java.rmi.RemoteException;</FONT></P>
<P><FONT size=2>public interface HaiClient extends EJBObject {<BR>&nbsp;&nbsp;&nbsp; public String sayHai() throws RemoteException;&nbsp;&nbsp; <BR>}</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>haiBean.java:</U></FONT></P>
<P><FONT size=2>package ejbs;</FONT></P>
<P><FONT size=2>import javax.ejb.*;<BR>import javax.naming.*;</FONT></P>
<P><FONT size=2>public class HaiBean implements SessionBean {<BR>&nbsp;&nbsp;&nbsp; public String sayHai() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "<FONT style="BACKGROUND-COLOR: #ffff00">Hai, EJB technology!";</FONT>&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; }&nbsp; <BR>&nbsp;&nbsp;&nbsp; public void ejbCreate() throws EJBException {}<BR>&nbsp;&nbsp;&nbsp; public void ejbRemove() throws EJBException {}<BR>&nbsp;&nbsp;&nbsp; public void ejbPassivate() {}<BR>&nbsp;&nbsp;&nbsp; public void ejbActivate() {} <BR>&nbsp;&nbsp;&nbsp; public void setSessionContext(SessionContext sc) {}<BR>}</FONT></P></FONT>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P>
<P><STRONG><FONT size=2>二、创建EJB组件：</FONT></STRONG></P>
<P><STRONG><FONT size=2></FONT></STRONG>&nbsp;</P>
<P><STRONG><FONT size=2>haiejb.jar:(EJB组件）</FONT></STRONG></P>
<P><FONT size=2><STRONG>打包命令：[EJB组件目录]&gt;:jar cvf haiejb.jar META-INF/ ejbs/</STRONG></FONT></P>
<P><FONT size=2>|__META-INF/</FONT></P>
<P><FONT size=2>|&nbsp; |__ejb-jar.xml</FONT></P>
<P><FONT size=2>|&nbsp; |__jboss.xml</FONT></P>
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT size=2>|__ejbs/</FONT></P>
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT size=2>&nbsp;&nbsp;&nbsp;|__HaiHome.class</FONT></P>
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT size=2>&nbsp;&nbsp; |__HaiClient.class</FONT></P>
<P dir=ltr style="MARGIN-RIGHT: 0px"><FONT size=2>&nbsp;&nbsp; |__HaiBean.class</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>ejb-jar.xml:</U></FONT></P>
<P><FONT size=2>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>&lt;!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'&gt;</FONT></P>
<P><FONT size=2>&lt;ejb-jar&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;description&gt;Hai EJB instance.&lt;/description&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;display-name&gt;Hai EJB&lt;/display-name&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;enterprise-beans&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;session&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/ejb-name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;home&gt;ejbs.HaiHome&lt;/home&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;remote&gt;ejbs.HaiClient&lt;/remote&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-class&gt;ejbs.HaiBean&lt;/ejb-class&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;session-type&gt;Stateless&lt;/session-type&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;transaction-type&gt;Bean&lt;/transaction-type&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/session&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/enterprise-beans&gt;<BR>&lt;/ejb-jar&gt;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>jboss.xml:</U></FONT></P>
<P><FONT size=2>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>&lt;jboss&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;enterprise-beans&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;session&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/ejb-name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;jndi-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/jndi-name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/session&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/enterprise-beans&gt;<BR>&lt;/jboss&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><STRONG>三、创建WEB应用</STRONG></FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><STRONG><FONT size=2>haiejb.war:(WEB应用）</FONT></STRONG></P>
<P><STRONG><FONT size=2>打包命令：[WEB应用目录]&gt;:jar cvf haiejb.war haiejb.jsp WEB-INF/</FONT></STRONG></P>
<P><FONT size=2>|__haiejb.jsp</FONT></P>
<P><FONT size=2>|__WEB-INF/</FONT></P>
<P><FONT size=2>&nbsp;&nbsp; |__web.xml</FONT></P>
<P><FONT size=2>&nbsp;&nbsp; |__jboss-web.xml</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>haiejb.jsp:</U></FONT></P>
<P><FONT size=2>&lt;%@ page contentType="text/html;charset=GBK" %&gt;<BR>&lt;%@ page import="ejbs.*,javax.ejb.*,javax.naming.*,javax.rmi.PortableRemoteObject,java.rmi.RemoteException" %&gt;</FONT></P>
<P><FONT size=2>&lt;html&gt;<BR>&lt;body&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;%<BR>&nbsp;&nbsp;&nbsp; String message = "nothing!";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitialContext ic = new InitialContext();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object objRef = ic.lookup("<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HaiHome home = (HaiHome) PortableRemoteObject.narrow(objRef,ejbs.HaiHome.class);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HaiClient haiRemote = home.create();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; message = haiRemote.sayHai();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (RemoteException re) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; re.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (CreateException ce) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ce.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (NamingException ne) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ne.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; %&gt;<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; &lt;h1&gt;&lt;%=message%&gt;&lt;/h1&gt;<BR>&lt;/body&gt; <BR>&lt;/html&gt;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>web.xml:</U></FONT></P>
<P><FONT size=2>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>&lt;!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'&gt;</FONT></P>
<P><FONT size=2>&lt;web-app&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;ejb-ref&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-ref-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/ejb-ref-name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-ref-type&gt;Session&lt;/ejb-ref-type&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;home&gt;ejbs.HaiHome&lt;/home&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;remote&gt;ejbs.HaiClient&lt;/remote&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/ejb-ref&gt;<BR>&lt;/web-app&gt;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>jboss-web.xml:</U></FONT></P>
<P><FONT size=2>&lt;?xml version="1.0" encoding="UTF-8"?&gt;<BR>&lt;jboss-web&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;ejb-ref&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb-ref-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/ejb-ref-name&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;jndi-name&gt;<FONT style="BACKGROUND-COLOR: #ffff00">HaiEJB</FONT>&lt;/jndi-name&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/ejb-ref&gt;<BR>&lt;/jboss-web&gt;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><STRONG>四、创建J2EE应用程序</STRONG></FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><STRONG><FONT size=2>haiejb.ear: (J2EE应用程序）</FONT></STRONG></P>
<P><STRONG><FONT size=2>把上面创建的haiejb.jar和haiejb.war包拷贝到你创建的J2EE应用主目录，新建一个META-INF目录并在里面创建application.xml文件：</FONT></STRONG></P>
<P><STRONG><FONT size=2>打包命令：[J2EE应用目录]&gt;:jar cvf haiejb.ear haiejb.jar haiejb.war META-INF/</FONT></STRONG></P>
<P><FONT size=2>|__haiejb.jar</FONT></P>
<P><FONT size=2>|__haiejb.war</FONT></P>
<P><FONT size=2>|__META-INF/</FONT></P>
<P><FONT size=2>&nbsp;&nbsp; |__application.xml</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><U>application.xml:</U></FONT></P>
<P><FONT size=2>&lt;?xml version="1.0" encoding="UTF-8"?&gt;</FONT></P>
<P><FONT size=2>&lt;application&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;display-name&gt;HaiEJB J2EE Application&lt;/display-name&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;module&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;web&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;web-uri&gt;haiejb.war&lt;/web-uri&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;context-root&gt;/haiejb&lt;/context-root&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/web&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/module&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;module&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ejb&gt;haiejb.jar&lt;/ejb&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;/module&gt;<BR>&lt;/application&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><STRONG>五、部署J2EE应用：</STRONG></P>
<P>&nbsp;</P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2><STRONG>把haiejb.ear拷贝到</STRONG></FONT></P>
<P><FONT size=2><STRONG>JBOSS_HOME\server\default\deploy\</STRONG></FONT></P>
<P><FONT size=2><STRONG>启动jboss 4.0,注意命令行窗口中有无异常，如果有异常情况，请查看log文件：</STRONG></FONT></P>
<P><FONT size=2><STRONG>JBOSS_HOME\server\default\log\server.log</STRONG></FONT></P>
<P><FONT size=2><STRONG>从中查找分析问题所在，然后改正之,直到无异常显示</STRONG></FONT></P>
<P><FONT size=2></FONT>&nbsp;</P>
<P><FONT size=2>最后在浏览器地址栏中键入：</FONT></P>
<P><FONT color=#002c99 size=2><A href="http://localhost:8080/haiejb/haiejb.jsp" target=_blank>http://localhost:8080/haiejb/haiejb.jsp</A></FONT></P>
<P><FONT color=#002c99 size=2></FONT>&nbsp;</P>
<P><FONT size=2>结果：</FONT></P>
<P>&nbsp;</P>
<P><FONT size=2><STRONG><FONT size=6>Hai, EJB technology!&nbsp;&nbsp;&nbsp;&nbsp;</FONT></STRONG>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P></DIV><img src ="http://www.blogjava.net/joeyeezhang/aggbug/29691.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/joeyeezhang/" target="_blank">joeyeezhang</a> 2006-02-06 13:19 <a href="http://www.blogjava.net/joeyeezhang/archive/2006/02/06/29691.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>