使用Glassfish服务器从JSP访问远程EJB3
我是J2EE/EJB领域的新手,我正在尝试使用JSP模块调用的无状态远程EJB3模块部署和运行一个简单的项目 这里是MyBean的远程业务接口使用Glassfish服务器从JSP访问远程EJB3,jsp,glassfish,java-ee-6,ejb-3.1,Jsp,Glassfish,Java Ee 6,Ejb 3.1,我是J2EE/EJB领域的新手,我正在尝试使用JSP模块调用的无状态远程EJB3模块部署和运行一个简单的项目 这里是MyBean的远程业务接口 package converter.ejb; import java.math.BigDecimal; import javax.ejb.Remote; @Remote public interface Converter { public BigDecimal dollarToYen(BigDecimal dollars); p
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);
}
这里是my 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>
我正在使用EclipseIDE,编译converter jsp项目的唯一方法是添加对项目converter ejb的引用。运行jsp应用程序的唯一方法是创建一个EAR项目,其中引用converter-ejb.jar文件和converter-jsp.war文件
我想知道我是否可以在不创建这个新的EAR项目的情况下部署我的应用程序,但同时将EJB与JSP代码分开。我试图在converter jsp项目中添加对converter-ejb.jar文件的引用,但在运行时,我收到一个异常,表示找不到converter类
更新2
我试图实现的目标在第39页中有描述。我希望在应用服务器上部署所有不同的模块,并在不创建和部署“EAR”应用程序的情况下运行应用程序
更新3
我不知道我想做的事是否可能。但是我认为这就像使用RMI远程调用远程EJB上的方法一样。您的
转换器jsp.jar
应该将EJB接口作为依赖项。我建议将您的接口放在不同的包中,并将它们作为jsp jar的依赖项
这些是否部署在不同的EAR/WAR文件中?你能描述一下包装结构吗
如果你更新了你的问题,我会相应地更新我的答案
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