|
ejb的名声很不好,尤其在spring框架出现以后,而ejb3似乎脱胎换骨了。而我现在学习的仍然是ejb2,原因是ejb3的书很难找。暑假时大概把《精通ejb》和《ejb》这两本书看过一下,现在想复习,加深理解,就从第一个helloworld开始。 文件结构: e:. │ build.xml │ └─src ├─meta-inf │ application.xml │ ejb-jar.xml │ jboss.xml │ ├─powerwind │ clientservlet.java │ hello.java │ hellobean.java │ hellohome.java │ └─web-inf jboss-web.xml web.xml ant文件 build.xml <?xml version="1.0"?> <project name="hello" default="prepare" basedir="."> <property environment="env" /> <property name="src.dir" value="${basedir}/src" /> <property name="jboss.home" value="${env.jboss_home}" /> <property name="build.dir" value="${basedir}/build" /> <property name="classes.dir" value="${build.dir}/classes" /> <property name="dist.dir" value="${basedir}/dist" /> <path id="compile.classpath"> <pathelement location="${classes.dir}" /> <fileset dir="${jboss.home}/client"> <include name="jboss-j2ee.jar" /> <include name="javax.servlet.jar" /> <include name="commons-httpclient.jar" /> </fileset> </path> <target name="prepare"> <mkdir dir="${classes.dir}" /> <mkdir dir="${dist.dir}" /> <copy todir="${build.dir}"> <fileset dir="${src.dir}"> <include name="web-inf/*.xml" /> <include name="meta-inf/*.xml" /> </fileset> </copy> </target> <target name="clean"> <delete dir="${build.dir}" /> <delete dir="${dist.dir}" /> </target> <target name="compile" depends="prepare"> <javac srcdir="${src.dir}" destdir="${classes.dir}"> <classpath refid="compile.classpath" /> </javac> </target> <target name="jar" depends="compile"> <jar destfile="${dist.dir}/helloejb.jar"> <fileset dir="${build.dir}"> <exclude name="meta-inf/application.xml" /> <include name="meta-inf/*.xml" /> </fileset> <fileset dir="${classes.dir}"> <include name="**/*.class" /> </fileset> </jar> </target> <target name="war" depends="jar"> <jar destfile="${dist.dir}/helloejb.war"> <fileset dir="${build.dir}"> <include name="web-inf/*.xml" /> </fileset> </jar> </target> <target name="ear" depends="war"> <jar destfile="${dist.dir}/helloejb.ear"> <fileset dir="${dist.dir}"> <include name="helloejb.jar" /> <include name="helloejb.war" /> </fileset> <fileset dir="${build.dir}"> <include name="meta-inf/application.xml" /> </fileset> </jar> </target> <target name="deploy" depends="ear"> <copy file="${dist.dir}/helloejb.ear" overwrite="true" todir="${jboss.home}/server/default/deploy" /> </target> </project>
java源文件 hello.java package powerwind; import java.rmi.remoteexception; import javax.ejb.ejbobject; publicinterface hello extends ejbobject { public string sayhello() throws remoteexception; } hellohome.java package powerwind; import java.rmi.remoteexception; import javax.ejb.createexception; import javax.ejb.ejbhome; publicinterface hellohome extends ejbhome { public hello create() throws remoteexception, createexception; } hellobean.java package powerwind; import java.rmi.remoteexception; import javax.ejb.ejbexception; import javax.ejb.sessionbean; import javax.ejb.sessioncontext; publicclass hellobean implements sessionbean { private sessioncontext context; publicvoid ejbcreate() throws ejbexception, remoteexception { } publicvoid ejbactivate() throws ejbexception, remoteexception { } publicvoid ejbpassivate() throws ejbexception, remoteexception { } publicvoid ejbremove() throws ejbexception, remoteexception { } publicvoid setsessioncontext(sessioncontext newcontext) throws ejbexception { context = newcontext; } public string sayhello() throws ejbexception { return"hello from ejb"; } } 用于测试的servlet clientservlet.java package powerwind; import java.io.ioexception; import java.io.printwriter; import javax.naming.initialcontext; import javax.rmi.portableremoteobject; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; publicclass clientservlet extends httpservlet { publicvoid doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html"); printwriter out = response.getwriter(); string message = null; try { initialcontext ic = new initialcontext(); object obj = ic.lookup("helloejb"); hellohome home = (hellohome) portableremoteobject.narrow(obj, powerwind.hellohome.class); hello hello = home.create(); message = hello.sayhello(); } catch (exception e) { e.printstacktrace(); } out.println(message); out.flush(); out.close(); } publicvoid dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { doget(request, response); } }
配置文件: 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>helloejb,my first ejb program</description> <display-name>helloejb</display-name> <enterprise-beans> <session> <ejb-name>helloejb</ejb-name> <home>powerwind.hellohome</home> <remote>powerwind.hello</remote> <ejb-class>powerwind.hellobean</ejb-class> <session-type>stateless</session-type> <transaction-type>container</transaction-type> </session> </enterprise-beans> </ejb-jar> jboss-jar <?xml version="1.0" encoding="utf-8"?> <jboss> <enterprise-beans> <session> <ejb-name>helloejb</ejb-name> <jndi-name>helloejb</jndi-name> </session> </enterprise-beans> </jboss> application.xml <?xml version="1.0"?> <application> <display-name>helloejb j2ee application</display-name> <module> <web> <web-uri>helloejb.war</web-uri> <context-root>/helloejb</context-root> </web> </module> <module> <ejb>helloejb.jar</ejb> </module> </application> jboss-web.xml <?xml version="1.0"?> <jboss-web> <ejb-ref> <ejb-ref-name>helloejb</ejb-ref-name> <jndi-name>helloejb</jndi-name> </ejb-ref> </jboss-web> web.xml <?xml version="1.0" encoding="utf-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>clientservlet</servlet-name> <servlet-class>powerwind.clientservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>clientservlet</servlet-name> <url-pattern>/clientservlet</url-pattern> </servlet-mapping> <ejb-ref> <ejb-ref-name>helloejb</ejb-ref-name> <ejb-ref-type>session</ejb-ref-type> <home>powerwind.hellohome</home> <remote>powerwind.hello</remote> </ejb-ref> </web-app> 输入 ant ?cdeploy 部署,启动jboss服务器,在浏览器地址栏输入: http://localhost:8080/helloejb/clientservlet 然后看到浏览器输出“hello from ejb”,就是成功了。
|