使用环境
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 -内部服务器错误
type Exception report
messageInternal Server Error
服务器遇到内部错误,无法完成此请求。
异常
javax.servlet。ServletException:实例化servlet类SphereServlet出错
根源com.sun.enterprise.container.common.spi.util.InjectionException:为类SphereServlet创建管理对象时出错
根源com.sun.enterprise.container.common.spi.util.InjectionException:试图注入远程ejb的异常-ref name=SphereServlet/sphere,Remote 3。. x interface =packt. spherebean,ejb-link=null,lookup=,mappedName=,jndi-name=packt。在SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl. myEnv]中,会话到类SphereServlet:查找'java:comp/env/SphereServlet/sphere'失败。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:在SerialContext中查找'java:comp/env/SphereServlet/sphere'失败[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。[根异常是javax.naming.NamingException:异常解析Ejb为'Remote Ejb -ref name=SphereServlet/sphere,Remote 3。. x interface =packt. spherebean,ejb-link=null,lookup=,mappedName=,jndi-name=packt。SphereBean refType =会话"。用于查找的实际(可能是内部的)远程JNDI名称是"packt. spherebean #packt"。SphereBean'[根异常是javax.naming.NamingException:查找失败的包。SphereBean#包。[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。[根异常是javax.naming.NameNotFoundException: packt. spherebean #packt。SphereBean未找到[]]
根源javax.naming。NamingException:为"远程Ejb -ref name=SphereServlet/sphere,Remote"解析Ejb的异常。. x interface =packt. spherebean,ejb-link=null,lookup=,mappedName=,jndi-name=packt。SphereBean refType =会话"。用于查找的实际(可能是内部的)远程JNDI名称是"packt. spherebean #packt"。SphereBean'[根异常是javax.naming.NamingException:查找失败的包。SphereBean#包。[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。[根异常是javax.naming.NameNotFoundException: packt. spherebean #packt。SphereBean未找到[]
根源javax.naming。NamingException:查找'packt. spherebean #packt失败。[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。[根异常是javax.naming.NameNotFoundException: packt. spherebean #packt。SphereBean未找到]
根源javax.naming。NameNotFoundException: packt.SphereBean # packt。SphereBean未找到
在GlassFish Server开源版4.1日志中可以找到异常的完整堆栈跟踪及其根本原因。GlassFish Server开源版4.1规范第4.9.8节规定:
下面是公开无接口视图的会话bean的要求:
bean类必须通过其bean类定义或在部署描述符中指定它公开无接口视图。以下规则适用:
如果bean不公开任何其他客户端视图(本地、远程、无接口),则2。2.远程家庭x Local Home, Web Service)及其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);
}
}
应该可以满足你的要求。
但是,如果你想实际使用Local/Remote接口,那么在注入时就应该使用这种类型:
@WebServlet(urlPatterns = {"/SphereServlet"})
public class SphereServlet extends HttpServlet {
@EJB
SphereBeanRemote sphere;
...