【发布时间】:2012-01-17 13:36:17
【问题描述】:
我是 J2EE/EJB 领域的新手,我正在尝试使用 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);
}
}
这两个类部署在一个包“converter.ejb”和一个jar文件“converter-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,编译converter-jsp 项目的唯一方法是添加对converter-ejb 项目的引用。运行我的 jsp 应用程序的唯一方法是使用对 converter-ejb.jar 文件和 converter-jsp.war 文件的引用创建一个 EAR 项目。
我想知道我是否可以在不创建这个新的 EAR 项目的情况下部署我的应用程序,但同时维护将 EJB 与 JSP 代码分开。我尝试在converter-jsp 项目中添加对我的converter-ejb.jar 文件的引用,但在运行时出现异常,提示找不到Converter 类。
更新 2
Oracle Application Deployment Guide 的第 39 页描述了我想要实现的目标。我想在应用程序服务器上部署所有不同的模块并运行应用程序,而无需创建和部署“EAR”应用程序。
更新 3 我不知道我想做的事情是否可行。但我认为这就像使用 RMI 远程调用远程 EJB 上的方法。
【问题讨论】:
标签: jsp glassfish java-ee-6 ejb-3.1