2012-01-17 88 views
0

我是J2EE/EJB領域的新手,我嘗試使用JSP模塊調用的無狀態遠程EJB3模塊部署和運行簡單項目。使用Glassfish服務器從JSP訪問遠程EJB3

這裏有我的bean的遠程業務接口

package converter.ejb; 

import java.math.BigDecimal; 

import javax.ejb.Remote; 

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

    public BigDecimal yenToEuro(BigDecimal yen); 
} 

這裏有我的bean的代碼:

package converter.ejb; 

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

@Stateless 
public class ConverterBean implements Converter { 
    private BigDecimal yenRate = new BigDecimal("83.0602"); 
    private BigDecimal euroRate = new BigDecimal("0.0093016"); 

    @Override 
    public BigDecimal dollarToYen(BigDecimal dollars) { 
     BigDecimal result = dollars.multiply(yenRate); 
     return result.setScale(2, BigDecimal.ROUND_UP); 
    } 

    @Override 
    public BigDecimal yenToEuro(BigDecimal yen) { 
     BigDecimal result = yen.multiply(euroRate); 
     return result.setScale(2, BigDecimal.ROUND_UP); 
    } 
} 

這2類被部署在一個包「converter.ejb」並在一個jar文件「轉換器-ejb.jar」

我想從這個JSP頁面訪問我的bean

<%@page import="java.util.Properties"%> 
<%@ page import="converter.ejb.Converter,java.math.*,javax.naming.*"%> 

<%!private Converter converter = null; 

    public void jspInit() { 
     try { 

      InitialContext ic = new InitialContext(); 
      converter = (Converter) ic.lookup(Converter.class.getName()); 
     } catch (Exception ex) { 
      System.out.println("Couldn't create converter bean." 
        + ex.getMessage()); 
     } 
    } 

    public void jspDestroy() { 
     converter = null; 
    }%> 
<html> 
<head> 
<title>Converter</title> 
</head> 

<body bgcolor="white"> 
    <h1>Converter</h1> 
    <hr> 
    <p>Enter an amount to convert:</p> 
    <form method="get"> 
     <input type="text" name="amount" size="25"> <br> 
     <p> 
      <input type="submit" value="Submit"> <input type="reset" 
       value="Reset"> 
    </form> 

    <% 
     String amount = request.getParameter("amount"); 
     if (amount != null && amount.length() > 0) { 
      BigDecimal d = new BigDecimal(amount); 

      BigDecimal yenAmount = converter.dollarToYen(d); 
    %> 
    <p> 
     <%=amount%> 
     dollars are 
     <%=yenAmount%> 
     Yen. 
    <p> 
     <% 
      BigDecimal euroAmount = converter.yenToEuro(yenAmount); 
     %> 
     <%=yenAmount%> 
     Yen are 
     <%=euroAmount%> 
     Euro. 
     <% 
      } 
     %> 

</body> 
</html> 

它被部署在另一個jar文件「converter-jsp.jar」中。

在運行時執行過程中,我收到一個異常,說找不到包「converter.ejb」和類「Converter」。

請幫幫我。

更新

我已經打包我的這種結構的應用:

converter-ejb.jar 
├── converter 
│   └── ejb 
│    ├── ConverterBean.class 
│    └── Converter.class 
└── META-INF 
    ├── ejb-jar.xml 
    ├── MANIFEST.MF 
    └── sun-ejb-jar.xml 

converter-jsp.war 
├── index.jsp 
├── META-INF 
│   └── MANIFEST.MF 
└── WEB-INF 
    ├── classes 
    ├── lib 
    └── sun-web.xml 

converter-jsp-app.ear 
├── converter-ejb.jar 
├── converter-jsp.war 
└── META-INF 
    ├── application.xml 
    └── MANIFEST.MF 

我與Eclipse IDE中的唯一途徑合作,以編譯器,JSP項目添加參考項目converter-ejb。運行我的jsp應用程序的唯一方法是創建一個EAR項目,其中引用了converter-ejb.jar文件和converter-jsp.war文件。

我想知道,如果我可以部署我的應用程序而不創建這個新的EAR項目,但在同一個維護從EJB代碼分離的EJB。我試圖在converter-jsp項目中添加一個對我的converter-ejb.jar文件的引用,但是在運行時,我收到一個異常,說無法找到Converter類。

更新2

我試圖實現在Oracle Application Deployment Guide的PAG 39描述。我想在應用程序服務器上部署所有不同的模塊,並運行應用程序而不創建和部署「EAR」應用程序。

更新3 我不知道我想做什麼是可能的或不可以。但我認爲這將會像使用RMI遠程調用遠程EJB上的方法。

回答

1

您的converter-jsp.jar應該將ejb接口作爲依賴項。我建議將你的接口放在不同的包中,並將它們作爲你的jsp jar的依賴。

這些部署在不同的EAR/WAR文件中嗎?你能描述包裝結構嗎?

如果你更新你的問題,我會相應地更新我的答案。

+0

我按照你的建議,我解決了這個問題。 – PinoSan 2012-01-19 23:39:08