俊星的BLOG

APACHE Geronimo 试用之EJB3

  Geronimo是APACHE出品的一个JEE应用服务器,支持JMS,JDBC,EJB等,下面是我的试用:
接口类:
package ejb;

import java.math.BigDecimal;
import javax.ejb.Remote;

@Remote
public interface Converter {
    
public BigDecimal dollarToRupees(BigDecimal dollars);

    
public BigDecimal rupeesToEuro(BigDecimal rupees);
}

BEAN类:
package ejb;

import java.math.BigDecimal;
import javax.ejb.*;

@Stateless
public class ConverterBean implements Converter {
    
private BigDecimal rupeeRate = new BigDecimal("40.58");
    
private BigDecimal euroRate = new BigDecimal("0.018368");

    
public BigDecimal dollarToRupees(BigDecimal dollars) {
        BigDecimal result 
= dollars.multiply(rupeeRate);
        
return result.setScale(2, BigDecimal.ROUND_UP);
    }


    
public BigDecimal rupeesToEuro(BigDecimal rupees) {
        BigDecimal result 
= rupees.multiply(euroRate);
        
return result.setScale(2, BigDecimal.ROUND_UP);
    }

}

引用EJB的Servlet:
package webejb;

import ejb.Converter;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ConverterHandler extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet 
{
    @EJB(name 
= "ejb/Converter")
    
private Converter converter;

    
protected void doGet(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        PrintWriter out 
= response.getWriter();
        String amount 
= request.getParameter("amount");
        
if (amount != null && amount.length() > 0{
            BigDecimal d 
= new BigDecimal(amount);
            BigDecimal rupeeAmount 
= converter.dollarToRupees(d);
            out.println(
"<p>" + amount + " Dollars are " + rupeeAmount
                    
+ " Rupees.<p>");
            BigDecimal euroAmount 
= converter.rupeesToEuro(rupeeAmount);
            out.println(amount 
+ " Dollars are " + euroAmount + " Euro.");
        }

    }


    
protected void doPost(HttpServletRequest request,
            HttpServletResponse response) 
throws ServletException, IOException {
        doGet(request, response);
    }

}

geronimo-web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" 
xmlns:nam
="http://geronimo.apache.org/xml/ns/naming-1.2" 
xmlns:sec
="http://geronimo.apache.org/xml/ns/security-2.0" 
xmlns:sys
="http://geronimo.apache.org/xml/ns/deployment-1.2">

    
<sys:environment>
        
<sys:moduleId>
            
<sys:groupId>default</sys:groupId>
            
<sys:artifactId>WebEJB</sys:artifactId>
            
<sys:version>1.0</sys:version>
            
<sys:type>car</sys:type>
        
</sys:moduleId>
        
<sys:dependencies>
            
<sys:dependency>
                
<sys:groupId>default</sys:groupId>
                    
<sys:artifactId>TestEJB</sys:artifactId>
                  
<sys:version>1.0</sys:version>
                  
<sys:type>car</sys:type>
            
</sys:dependency>        
        
</sys:dependencies>
    
</sys:environment>

    
<context-root>/WebEJB</context-root>

    
<nam:ejb-ref xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2">
        
<nam:ref-name>ejb/Converter</nam:ref-name>
        
<nam:pattern>
            
<nam:groupId>default</nam:groupId>
            
<nam:artifactId>TestEJB</nam:artifactId>
            
<nam:version>1.0</nam:version>
            
<nam:name>ConverterBean</nam:name>
        
</nam:pattern>
    
</nam:ejb-ref>

</web-app>

WEB.XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns
="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id
="WebApp_ID" version="2.5">
    
<display-name>WebEJB</display-name>
    
<welcome-file-list>
        
<welcome-file>index.jsp</welcome-file>
    
</welcome-file-list>
    
<servlet>
        
<description></description>
        
<display-name>ConverterHandler</display-name>
        
<servlet-name>ConverterHandler</servlet-name>
        
<servlet-class>webejb.ConverterHandler</servlet-class>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>ConverterHandler</servlet-name>
        
<url-pattern>/ConverterHandler</url-pattern>
    
</servlet-mapping>
</web-app>


测试页面index.jsp:

<html>
<head>
<title>Converter</title>
</head>
<body bgcolor="white">
<h1>Converter</h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get" action="index.jsp"><input type="text"
    name
="amount" size="25"><br>
<p><input type="submit" value="Submit"> <input type="reset"
    value
="Reset">
</form>
<jsp:include page="/ConverterHandler" />
</body>


发布应用:
1:发布包含EJB文件的TestEJB.jar
2:发布WEB应用WebEJB.war
具体文件请点击此处下载:http://www.blogjava.net/Files/kinkding/Geromino_EJB_Test.rar
发布成功后,通过点击http://localhost:8080/WebEJB即可看到程序运行效果。

posted on 2009-05-12 21:40 俊星 阅读(262) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: