分享
 
 
 

Jboss4.x下第一个EJB2.x程序

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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>

更多的请看:http://www.QQread.com/windows/2003/index.Html

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);

}

}

更多的请看:http://www.qqread.com/windows/2003/index.html

配置文件:

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 –deploy 部署,启动Jboss服务器,在浏览器地址栏输入:

http://localhost:8080/HelloEJB/ClientServlet

然后看到浏览器输出“Hello from ejb”,就是成功了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有