MyEclipse+JBoss开发EJB3.0
1. 配置环境:下载JBoss我用的是jboss4.0.5GA
http://umn.dl.sourceforge.net/sourceforge/jboss/jboss-4.0.5.GA.zip
然后下载jboss-EJB-3.0_RC9_Patch_1我用的是
http://nchc.dl.sourceforge.net/sourceforge/jboss/jboss-EJB-3.0_RC9_Patch_1.zip
设置环境变量:JBOSS_HOME=D:\jboss-4.0.5.GA;
下载ant1.7用ant部署jboss-EJB-3.0_RC9_Patch_1
设置ANT_HOME= C:\apache-ant-1.7.0
Path里添加%ANT_HOME%\bin 然后打开jboss-EJB-3.0_RC9_Patch_1
文件夹下的install.html 打开命令行
cd jboss-EJB-3.0_RC9_Patch1-FD
ant -f install.xml -Djboss.server.config=all 这样就部署到jboss下了
接下来就是切换到jboss4.0.5GA\bin目录下运行run –c all 看到这样的信息说明你环境配置成功了
再看看http://localhost:8080/看看是否用jboss的欢迎页面
2.开始我们的第一个ejb工程吧
新建一个WEB工程命名为firstEjb
接着建package 为com.ejb
新建一个接口为HelloWorld 建立方法为sayHello();
package com.ejb;
public interface HelloWorld {
public String sayHello(String who);
}
接着新建class HelloWorldBean.java
package com.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote({HelloWorld.class})
public class HelloWorldBean implements HelloWorld {
public String sayHello(String who) {
// TODO Auto-generated method stub
return who+",你好,欢迎使用ejb";
}
}
新建个servlet 为firstEjb.java
部分源码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html,charset=GBK");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx;
try {
ctx = new InitialContext(props);
HelloWorld helloworld = (HelloWorld)
ctx.lookup("HelloWorldBean/remote");
out.println(helloworld.sayHello("小树"));
} catch (NamingException e) {
out.println(e.getMessage());
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
打成jar包放到D:\jboss-4.0.5.GA\server\all\deploy下
最后就是发布程序了
<?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>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>firstEjb</servlet-name>
<servlet-class>com.servlet.firstEjb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstEjb</servlet-name>
<url-pattern>/firstEjb</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在网页中输入
http://localhost:8080/ejb/firstEjb你会看到你要的结果