Java EJB注入-JNDI查找失败

Java EJB注入-JNDI查找失败,java,jakarta-ee,netbeans,jndi,lookup,Java,Jakarta Ee,Netbeans,Jndi,Lookup,因此,我开始用Netbeans 8.2开发一个JavaEE企业应用程序。它是关于一个商店,你有一个购物车,你必须正确地存储每个客户的会话 在NetBeans中,我有:shopejb和shopwar Shop ejb包含以下类: CartLocal.java package cart; @Local public interface CartLocal{ } package cart; @Stateful @StatefulTimeout(unit = TimeUnit.MINUTES,

因此,我开始用Netbeans 8.2开发一个JavaEE企业应用程序。它是关于一个商店,你有一个购物车,你必须正确地存储每个客户的会话

在NetBeans中,我有:
shopejb
shopwar

Shop ejb
包含以下类:

  • CartLocal.java

    package cart;
    
    @Local
    public interface CartLocal{
    }
    
    package cart;
    
    @Stateful
    @StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)
    public class CartBean implements CartLocal{
    }
    
    import cart.CartLocal;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.*;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    @WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
    public class Servlet extends HttpServlet {
    
    private static final String CARRITO_SESSION_KEY = "shoppingCart";
    
    public Servlet() {
        super();
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        CartLocal carrito = (CartLocal) request.getSession().getAttribute(CARRITO_SESSION_KEY);
    
        try {
            if (carrito == null) {
                InitialContext ic = new InitialContext();
                carrito = (CartLocal) ic.lookup("java:global/Shop/Shop-ejb/CartBean!cart.CartLocal");
    
                request.getSession().setAttribute(CARRITO_SESSION_KEY, carrito);
            }
        } catch (NamingException ex) {
            Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
  • CartBean.java

    package cart;
    
    @Local
    public interface CartLocal{
    }
    
    package cart;
    
    @Stateful
    @StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)
    public class CartBean implements CartLocal{
    }
    
    import cart.CartLocal;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.*;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    @WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
    public class Servlet extends HttpServlet {
    
    private static final String CARRITO_SESSION_KEY = "shoppingCart";
    
    public Servlet() {
        super();
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        CartLocal carrito = (CartLocal) request.getSession().getAttribute(CARRITO_SESSION_KEY);
    
        try {
            if (carrito == null) {
                InitialContext ic = new InitialContext();
                carrito = (CartLocal) ic.lookup("java:global/Shop/Shop-ejb/CartBean!cart.CartLocal");
    
                request.getSession().setAttribute(CARRITO_SESSION_KEY, carrito);
            }
        } catch (NamingException ex) {
            Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
然后,
shopwar
包含以下servlet:

  • Servlet.java

    package cart;
    
    @Local
    public interface CartLocal{
    }
    
    package cart;
    
    @Stateful
    @StatefulTimeout(unit = TimeUnit.MINUTES, value = 20)
    public class CartBean implements CartLocal{
    }
    
    import cart.CartLocal;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.*;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.*;
    
    @WebServlet(name = "Servlet", urlPatterns = {"/Servlet"})
    public class Servlet extends HttpServlet {
    
    private static final String CARRITO_SESSION_KEY = "shoppingCart";
    
    public Servlet() {
        super();
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        CartLocal carrito = (CartLocal) request.getSession().getAttribute(CARRITO_SESSION_KEY);
    
        try {
            if (carrito == null) {
                InitialContext ic = new InitialContext();
                carrito = (CartLocal) ic.lookup("java:global/Shop/Shop-ejb/CartBean!cart.CartLocal");
    
                request.getSession().setAttribute(CARRITO_SESSION_KEY, carrito);
            }
        } catch (NamingException ex) {
            Logger.getLogger(Servlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
我正在使用GlasshFish,当我访问
http://localhost:8080/Shop-war/Servlet
我在NetBeans控制台上遇到以下错误:

Grave:   javax.naming.NamingException: Lookup failed for 'java:global/Shop/Shop-ejb/CartBean!cart.CartLocal' 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: Shop]

我知道
查找
失败,但我尝试了多种方法,但都没有结果。希望您能帮助我。

您需要在web.xml上设置一个
标记:

例如:

<ejb-local-ref>
        <ejb-ref-name>CartLocal</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local>cat.CartLocal</local>
        <ejb-link>CartLocal</ejb-link>
</ejb-local-ref>

谢谢你的回答,修复了我的错误,但我不明白为什么这是有效的,而我的代码不是