使用环境
Netbeans 8.0 +
glassfish 4 +
jdk 1.8
请注意,如果您不使用接口视图,此示例效果很好。如果我们使用本地或远程接口,则效果不佳
远程界面
package packt;
import javax.ejb.Remote;
@Remote
public interface SphereBeanRemote {
public double computeVolume(double radius);
}
以上界面的实施
package packt;
import javax.ejb.Stateless;
@Stateless
public class SphereBean implements SphereBeanRemote {
@Override
public double computeVolume(double radius) {
return (4.0/3.0)*Math.PI*(radius*radius*radius);
}
}
访问它的客户端(servlet)
import java.io.*;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import packt.SphereBean;
@WebServlet(urlPatterns = {"/SphereServlet"})
public class SphereServlet extends HttpServlet {
@EJB
SphereBean sphere;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SphereServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet SphereServlet at " + request.getContextPath() + "</h1>");
out.println("<h1>" + sphere.computeVolume(18) + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
错误 HTTP状态500 - 内部服务器错误
输入例外报告
messageInternal Server Error
description服务器遇到内部错误,导致无法完成此请求。
例外
javax.servlet.ServletException:实例化servlet类SphereServlet时出错
根本原因
com.sun.enterprise.container.common.spi.util.InjectionException:为类创建托管对象时出错:class SphereServlet
根本原因
com.sun.enterprise.container.common.spi.util.InjectionException:异常尝试注入Remote ejb-ref name = SphereServlet / sphere,Remote 3.x interface = packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi-name = packt.SphereBean,refType =类SphereServlet中的Session:查找失败的&#39; java:comp / env / SphereServlet / sphere&#39;在SerialContext中[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming}
根本原因
javax.naming.NamingException:&#39; java:comp / env / SphereServlet / sphere&#39;查找失败在SerialContext中[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NamingException:异常解析Ejb for&#39; Remote ejb-ref name = SphereServlet / sphere,Remote 3.x interface = packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi-name = packt.SphereBean,refType = Session&#39; 。用于查找的实际(可能是内部)远程JNDI名称是&#39; packt.SphereBean#packt.SphereBean&#39; [根异常是javax.naming.NamingException:查找失败了&#39; packt.SphereBean#packt.SphereBean&#39;在SerialContext中[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NameNotFoundException:packt.SphereBean#packt.SphereBean not found]]]
根本原因
javax.naming.NamingException:为远程ejb-ref name = SphereServlet / sphere解析Ejb的异常,Remote 3.x interface = packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi -name = packt.SphereBean,RefType的=会话&#39; 。用于查找的实际(可能是内部)远程JNDI名称是&#39; packt.SphereBean#packt.SphereBean&#39; [根异常是javax.naming.NamingException:查找失败了&#39; packt.SphereBean#packt.SphereBean&#39;在SerialContext中[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NameNotFoundException:packt.SphereBean#packt.SphereBean not found]]
根本原因
javax.naming.NamingException:&#39; packt.SphereBean#packt.SphereBean&#39;在SerialContext中[myEnv = {java.naming.factory.initial = com.sun.enterprise.naming.impl.SerialInitContextFactory,java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs = com.sun.enterprise.naming} [根异常是javax.naming.NameNotFoundException:packt.SphereBean#packt.SphereBean not found]
根本原因
javax.naming.NameNotFoundException:找不到packt.SphereBean#packt.SphereBean
注意GlassFish Server Open Source Edition 4。1日志中提供了异常的完整堆栈跟踪及其根本原因。 GlassFish Server开源4.1版
答案 0 :(得分:1)
以下是公开无界面视图的会话bean的要求:
bean类必须指定它通过bean类定义或部署描述符公开无接口视图。以下规则适用:
如果bean没有公开任何其他客户端视图(本地,远程,无接口,2.x远程主页,2.x本地主页,Web服务)并且其implements子句为空,则bean定义无界面视图。
如果bean公开了至少一个其他客户端视图,那么bean指定它通过bean类或部署描述符中的LocalBean注释公开无接口视图。
因此,改变您的实施:
package packt;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
@LocalBean
@Stateless
public class SphereBean implements SphereBeanRemote {
@Override
public double computeVolume(double radius) {
return (4.0/3.0)*Math.PI*(radius*radius*radius);
}
}
应该为你做的伎俩。
但是,如果您想要实际使用本地/远程接口,那么这是您在注入时应该使用的类型:
@WebServlet(urlPatterns = {"/SphereServlet"})
public class SphereServlet extends HttpServlet {
@EJB
SphereBeanRemote sphere;
...