JBossIDE Eclispe 开发EJB2.1实例

EJB2.1开发实例
Create by:forevering  2005-10-27
1.环境配置
1)JDK1.5 文件名:jdk-1_5_0_05-windows-i586-p.exe
下载地址:
https://jsecom16k.sun.com/ECom/EComActionServlet;jsessionid=31A76905496B86DCF9BCFE1CACE79895
2)编译器采用JBoss Eclipse IDE1.5RC1 文件名:JBossIDE-1.5RC1-Bundle-win32.zip
下载网址:http://www.jboss.com/products/jbosside/downloads
3)应用服务器:JBoss4.0.3 (JBoss4.0版本集成Tomcat) 文件名:jboss-4.0.3-installer.jar
下载网址:http://www.jboss.com/products/jbossas/downloads
4)安装jdk-1_5_0_05-windows-i586-p.exe,将下载好的JBossIDE-1.5RC1-Bundle-win32.zip解压都到某个目录即可,本例D:\eclipse;双击jboss-4.0.3-installer.jar即可安装(注意:不要将.jar文件与winrar建立关联),本例安装目录D:\jboss-4.0.3。
5)设置环境变量,本例环境变量如下:
JAVA_HOME= C:\Program Files\Java\jdk1.5.0
JBOSS_HOME=D:\jboss-4.0.3
Path=%JAVA_HOME%\bin;%JBOSS_HOME%\bin;
Classpath=%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;
6)启动JBoss,在命令行中输入:
>cd D:\jboss-4.0.3\bin 回车进入bin目录
D:\jboss-4.0.3\bin>run.bat 回车运行jboss
如图:
即可看到JBoss启动画面,如图
       在IE浏览器中输入:http://localhost:8080/ 回车即可看到JBoss欢迎页面
至此,环境搭建完毕。
2.开发EJB应用
2.1 开发Session Bean
1)启动eclipse
2)建立J2EE1.4项目,File->New->Project
点击 Next,输入项目名称,本例为Fibonaci
点击 Next,在Source一栏中点击 Add Folder,输入src用来存放项目的源码、配置文件等。
点击Finish项目建立完成,目录结构如下图:
3)创建Session Bean,点击src New->Other..
选择Session Bean,点击 Next
输入包名:fibonaci.ejb,文件名:FiboBean
点击 Finish完成Session Bean,目录结构如下
添加业务逻辑方法,点击FiboBean J2EE->Add Business Method
方法名:compute 数据类型:double[],参数int number[]
点击Finish完成
代码如下:
public double[] compute(int number) {
              if (number < 0) {
                     throw new EJBException("Argument should be positive");
              }           
              double[] suite = new double[number + 1];
              suite[0] = 0;          
              if (number == 0) {
                     return suite;
              }           
              suite[1] = 1;          
              for (int i = 2; i <= number; i++) {
                     suite[i] = suite[i - 1] + suite[i - 2];
              }
              return suite;
}
2.2 XDoclet设置EJB配置
1)EJB设置
点击项目右键->Properties->XDoclet Configurations->Add…
选中EJB,添加ejbdoclet
设置ejbdoclet属性:a) destDir为src b)ejbSpec为2.1
添加fileset,属性:a) dir为src b) includes为**/*Bean.java
添加deploymentdescriptor,属性 destDir为src/META-INF
添加jboss,属性:a) Version为4.0 b) destDir为src/META-INF
添加packageSubstitution,属性a) package为ejb b)substituteWith为interfaces
添加homeinterface和remoteinterface,不需要设置属性
点击OK,选中项目右键Run XDoclet
















可以看到Console信息





运行成功后,项目中
fibonaci.interfaces和META-INF被创建。
至此,ejb创建完成。









2.3开发Servlet
创建HTTP Servlet

输入Package:fibonaci.web Name:ComputeServlet,选中init()/service()/doPost()方法,点击Finish。
在项目中看到ComputeServlet.java,添加代码
ComputeServlet.java代码:
package fibonaci.web;

import javax.rmi.PortableRemoteObject;
import javax.servlet.http.HttpServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.Context;
import javax.naming.InitialContext;
import fibonaci.interfaces.Fibo;
import fibonaci.interfaces.FiboHome;

public class ComputeServlet extends HttpServlet {
       private FiboHome home;
       public ComputeServlet() {
              super();
       }
       public void init(ServletConfig config) throws ServletException {
              try {
                     Context context = new InitialContext();
                     Object ref = context.lookup("java:/comp/env/ejb/Fibo");
                     home = (FiboHome)PortableRemoteObject.narrow(ref, FiboHome.class);
              } catch (Exception e) {
                     throw new ServletException("Lookup of java:/comp/env/ failed");
              }
       }
       protected void doPost(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
              resp.setContentType("text/html");
              PrintWriter out = resp.getWriter();
             
              out.println("<html><head><title>");
              out.println("Fibonaci Computation");
              out.println("</title></head>");
              out.println("<body>");
             
              out.println("<h1>");
              out.println("Fibonaci Computation");
              out.println("</h1>");
             
              try {
                     Fibo bean = home.create();
                     int limit = 0;
                     String value = req.getParameter("limit");
                     if (value != null) {
                            try {
                                   limit = Integer.parseInt(value);                                 
                            } catch (Exception e) {                             
                            }
                     }
                     double[] result = bean.compute(limit);
                     bean.remove();
                    
                     out.println("<p>");
                     out.print("The ");
                     out.print(limit);
                     out.print(" first Fibonacci numbers ");
                     for (int i = 0; i < result.length; i++) {
                            out.println("<br>");
                            out.println(i);
                            out.println(" : ");
                            out.println(result[i]);
                     }
                     out.println("</p>");
                    
              } catch(Exception e) {
                     out.println(e.getMessage());
                     e.printStackTrace(out);
              } finally {
                     out.println("</body></html>");
                     out.close();
              }
       }
}
代码添加完毕后,需要添加XDoclet tags,在开始注释处输入@web,如下图:
设置内容如下:
/**
* Servlet Class
* @web.servlet     name="Compute"
*                 display-name="Computation Servlet"
*                 description="Servlet that compute Fibonacci suite"
* @web.servlet-mapping      url-pattern="/Compute"
* @web.ejb-ref  name="ejb/Fibo"
*              type="Session"
*              home="fibonacci.interfaces.FiboHome"
*              remote="fibonacci.interfaces.Fibo"
* @web.servlet-init-param   name="A parameter"
*                           value="A value"                          
* @jboss.ejb-ref-jndi   ref-name="ejb/Fibo"
*                   jndi-name="ejb/Fibo"                       
*/
2.4 XDoclet设置web配置
添加Web/webdoclet,属性destDir为src/WEB-INF
添加fileset,属性:a) di为src b) includes为**/*Servlet.java
添加deploymentdescriptor,属性:Servletspec为2.4
添加jbosswebxml,属性:Version为4.0
点击OK,XDoclet设置完成。点击项目右键->Run XDoclet。
运行成功后,WEB-INF被创建到项目中。
创建docroot目录,在docroot目录中创建index.html
index.html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>   
    <head>
       <title>Fibonacci Application</title>
    </head>
    <body bgcolor="#FFFFFF">
       <h1>Fibonacci Form</h1>
       <form action="Compute" method="POST">
           <table cellspacing="2" cellpadding="2" border="0">
              <tr>
                  <td>Limit :</td>
                  <td>
                     <input type="text" name="limit"
                            value="50"></input>
                  </td>
              </tr>
              <tr>
                  <td>
                     <input type="submit" name="Compute"
value="Compute"></input>
                  </td>
                  <td>                    
                  </td>
              </tr>
           </table>
       </form>
    </body>   
</html>
3 部署文件设置
3.1创建application.xml
点击src/META-INF目录,右键New->Other,选择EAR1.4 Deployment Descriptor
application.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<application version="1.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/application_1_4.xsd">
    <display-name>Sum Application</display-name>
    <module>
       <ejb>FiboEJB.jar</ejb>
    </module>
    <module>
       <web>
           <web-uri>FiboWeb.war</web-uri>
           <context-root>/fibo</context-root>
       </web>
    </module>
</application>
3.2创建FiboEJB.jar
点击项目右键->Properties->Packaging Configurations
添加目录
点击Project Folder,选择bin目录
Includes输入:fibonacci/ejb/*.class,fibonacci/interfaces/*.class
添加文件
点击Project File,选择ejb-jar.xml
Prefix输入:META-INF
同样方式添加jboss.xml
3.3创建FiboEJB-client.jar
添加Folder:/Fibonacci/bin,Includes:fibonacci/interfaces/*.class
3.4创建FiboWeb.war
添加Folder:/Fibonacci/bin,Includes:fibonacci/web/*.class Prefix:WEB-INF/classes
添加File:/Fibonacci/src/WEB-INF/web.xml,Prefix:WEB-INF
添加File:/Fibonacci/src/WEB-INF/jboss-web.xml,Prefix:WEB-INF
添加File:/Fibonacci/FiboEJB-client.jar,(如果没有列出,可以手动输入),Prefix:WEB-INF/lib
添加Floder:/Fibonacci/docroot
    设置完毕,如下图所示:
3.5创建FiboApp.ear
添加File:/Fibonacci/src/META-INF/application.xml,Prefix:META-INF
添加File:/Fibonacci/FiboEJB.jar
添加File:/Fibonacci/FiboWeb.war
创建完毕,如下图所示:
3.6 Run Pakaging
点击项目右键->Run Pakaging,可以看到Console信息:
运行成功,可以在项目中看到生成的包文件:FiboEJB.jar,FiboEJB-client.jar,FiboWeb.war,FiboApp.ear,其中FiboApp.ear为JBoss部署文件,如图:
3.7部署应用
点击项目,右键->Debug AS->Debug
选择应用服务器,本例JBoss4.0.3
添加资源,选择Source->Add->Java Project。
添加完后,点击Debug,可以在Console中看到JBoss启动信息。
启动成功后,点击FiboApp.ear,右键->Deployment->Deploy To
部署成功后可以在Console看到如下图:
部署成功打开IE浏览器,在地址栏输入:http://localhost:8080/fibo,可以看到页面:
点击Compute,操作成功后返回页面:
至此,EJB开发、部署完毕。

4部署描述符文件
4.1 ejb-jar.xml
<ejb-jar  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/ejb-jar_2_1.xsd" version="2.1">

   <description>Fibonacci EJB</description>
   <display-name>Generated by XDoclet</display-name>
   <enterprise-beans>
      <session >
         <description>Fibonacci EJB></description>
         <display-name>Name for Fibo</display-name>
         <ejb-name>Fibo</ejb-name>
         <home>fibonacci.interfaces.FiboHome</home>
         <remote>fibonacci.interfaces.Fibo</remote>
         <ejb-class>fibonacci.ejb.FiboBean</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>
      </session>
   </enterprise-beans>
   <assembly-descriptor >
   </assembly-descriptor>
</ejb-jar>
4.2 jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
   <enterprise-beans>
      <session>
         <ejb-name>Fibo</ejb-name>
         <jndi-name>ejb/Fibo</jndi-name>
        <method-attributes>
        </method-attributes>
      </session>
   </enterprise-beans>    
   <assembly-descriptor>
   </assembly-descriptor>
   <resource-managers>
   </resource-managers>
</jboss>
4.3 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app  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" version="2.4">
   <distributable/>
      <servlet>
      <description>Servlet that compute Fibonacci suite</description>
      <display-name>Computation Servlet</display-name>
      <servlet-name>Compute</servlet-name>
      <servlet-class>fibonacci.web.ComputeServlet</servlet-class>
      <init-param>
         <param-name>A parameter</param-name>
         <param-value>A value</param-value>
      </init-param>
   </servlet>
     <servlet-mapping>
      <servlet-name>Compute</servlet-name>
      <url-pattern>/Compute</url-pattern>
   </servlet-mapping>
   <ejb-ref >
      <ejb-ref-name>ejb/Fibo</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <home>fibonacci.interfaces.FiboHome</home>
      <remote>fibonacci.interfaces.Fibo</remote>
   </ejb-ref>
</web-app>
4.4 jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.4//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd">
<jboss-web>  
   <ejb-ref>
      <ejb-ref-name>ejb/Fibo</ejb-ref-name>
      <jndi-name>ejb/Fibo</jndi-name>
   </ejb-ref>
</jboss-web>
4.3 application.xml
<?xml version="1.0" encoding="UTF-8"?>
<application version="1.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/application_1_4.xsd">
    <display-name>Sum Application</display-name>
    <module>
       <ejb>FiboEJB.jar</ejb>
    </module>
    <module>
       <web>
           <web-uri>FiboWeb.war</web-uri>
           <context-root>/fibo</context-root>
       </web>
    </module>
</application>
5参考资料
[1] JBossIDE-Tutorial-1.4.1.pdf
[2] JBoss Meets Eclipse: Introducing the JBoss-IDE
http://www.devx.com/opensource/Article/20242/1954?pf=true
  [3] Tutorial for building J2EE Applications using JBOSS and ECLIPSE
http://www.tusc.com.au/tutorial/html/chap1.html
  [4] http://java.sun.com/xml/ns/j2ee/
  [5] http://www.eclipse.org/
  [6] http://www.jboss.org/

你可能感兴趣的:(eclipse,Web,jboss,servlet,ejb)