添加界面(本地和远程)时无法运行ejb应用程序

使用的环境

Netbeans 8.0+

玻璃鱼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服务器错误

description服务器遇到一个内部错误,导致它无法完成此请求。

例外

javax.servlet.ServletException:实例化servlet类SphereServlet时出错

根本原因

com.sun.enterprise.container.common.spi.util.InjectionException:为类创建托管对象时出错:class SphereServlet

根本原因

com.sun.enterprise.container.common.spi.util.InjectionException:尝试注入远程ejb-ref名称的异常= SphereServlet / sphere,远程3.x接口= packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi-name = packt.SphereBean,refType = Session SphereServlet: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.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.naming} [根异常是javax.naming.NamingException:异常解析Ejb for'Remote ejb-ref name = SphereServlet / sphere,Remote 3.x interface = packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi-name = packt.SphereBean,refType = Session'。 用于查找的实际(可能是内部的)远程JNDI名称是'packt.SphereBean#packt.SphereBean'[根异常是javax.naming.NamingException:SerialContext中的'packt.SphereBean#packt.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.naming} [根异常是javax.naming.NameNotFoundException:未找到packt.SphereBean#packt.SphereBean]]]

根本原因

javax.naming.NamingException:为远程ejb-ref名称= SphereServlet / sphere解析Ejb的异常,远程3.x接口= packt.SphereBean,ejb-link = null,lookup =,mappedName =,jndi-name = packt.SphereBean ,refType = Session'。 用于查找的实际(可能是内部的)远程JNDI名称是'packt.SphereBean#packt.SphereBean'[根异常是javax.naming.NamingException:SerialContext中的'packt.SphereBean#packt.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.naming} [根异常是javax.naming.NameNotFoundException:packt.SphereBean#packt.SphereBean未找到]]

根本原因

javax.naming.NamingException:在SerialContext中'packt.SphereBean#packt.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.naming} [根异常是javax.naming.NameNotFoundException:packt.SphereBean#未找到packt.SphereBean]

根本原因

javax.naming.NameNotFoundException:未找到packt.SphereBean#packt.SphereBean

note注意GlassFish Server Open Source Edition 4.1日志中提供了异常的完整堆栈跟踪及其根本原因。 GlassFish Server开源版4.1


说明书的第4.9.8节指出:

以下是暴露无界面视图的会话bean的要求:

  • bean类必须指定它通过它的bean类定义或部署描述符公开一个无接口视图。 以下规则适用:

  • 如果bean没有公开任何其他客户端视图(本地,远程,无界面,2.x远程主页,2.x本地主页,Web服务)及其实现子句为空,则该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;
    
        ...
    
    链接地址: http://www.djcxy.com/p/58785.html

    上一篇: Unable to run ejb application when interface (local and remote ) is added

    下一篇: how can use @EJB with remote interface"