Unable to run ejb application when interface (local and remote ) is added

Environment used

Netbeans 8.0+

glassfish 4+

jdk 1.8

Note this example works well if you use no interface view .It does not works well if we use local or remote interface

Remote interface

package packt;
import javax.ejb.Remote;
@Remote
public interface SphereBeanRemote {
     public double computeVolume(double radius);
}

implementation of above interface

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);
    }

}

client(servlet) which access it

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";
    }

}

Error HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class SphereServlet

root cause

com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class SphereServlet

root cause

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=SphereServlet/sphere,Remote 3.x interface =packt.SphereBean,ejb-link=null,lookup=,mappedName=,jndi-name=packt.SphereBean,refType=Session into class SphereServlet: Lookup failed for 'java:comp/env/SphereServlet/sphere' in 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}

root cause

javax.naming.NamingException: Lookup failed for 'java:comp/env/SphereServlet/sphere' in 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} [Root exception is javax.naming.NamingException: Exception resolving 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' . Actual (possibly internal) Remote JNDI name used for lookup is 'packt.SphereBean#packt.SphereBean' [Root exception is javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in 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} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]]

root cause

javax.naming.NamingException: Exception resolving 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' . Actual (possibly internal) Remote JNDI name used for lookup is 'packt.SphereBean#packt.SphereBean' [Root exception is javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in 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} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]]

root cause

javax.naming.NamingException: Lookup failed for 'packt.SphereBean#packt.SphereBean' in 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} [Root exception is javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found]

root cause

javax.naming.NameNotFoundException: packt.SphereBean#packt.SphereBean not found

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs. GlassFish Server Open Source Edition 4.1


§4.9.8 of the specification states that:

The following are the requirements for a session bean that exposes a no-interface view:

  • The bean class must designate that it exposes a no-interface view via its bean class definition or in the deployment descriptor. The following rules apply:

  • If the bean does not expose any other client views (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, Web Service) and its implements clause is empty, the bean defines a no-interface view.

  • If the bean exposes at least one other client view, the bean designates that it exposes a no-interface view by means of the LocalBean annotation on the bean class or in the deployment descriptor.

  • Therefore, changing your implementation:

    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);
        }
    }
    

    should do the trick for you.

    If however, you want to actually use the Local/Remote interface then that is the type you should use when injecting:

    @WebServlet(urlPatterns = {"/SphereServlet"})
    public class SphereServlet extends HttpServlet {
    
        @EJB
        SphereBeanRemote sphere;
    
        ...
    
    链接地址: http://www.djcxy.com/p/58786.html

    上一篇: 无法解析引用本地ejb

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