无法找到EJB

时间:2015-10-05 22:03:42

标签: java java-ee servlets ejb

我正在尝试使用JavaBeans或第一次,我得到以下内容 错误:

  

javax.naming.NameNotFoundException:DemoEJB

     

javax.naming.NamingException:查找失败   的Java:全球/ DemoEJB / ContenedorEJB / BeanStateful beanssesion.BeanStatefulLocal“   在SerialContext中[myEnv = {java.naming.factory

我正在使用带有glassfish 4.1服务器的NetBeans。 应用程序非常简单,我有一个企业应用程序项目,我链接一个EJB模块(称为ContenedorEJB)和一个客户端(它是一个Servlet)。

这是项目结构:

  • ClienteWeb

    • 源包

      -servlets

      -ServletEJB.java

  • ContenedorEJB

    -Source Packages

    • beanssesion

      • BeanStateful.java

      • BeanStatefulLocal.java

  • DemoEJB

    -Java EE Modules

    • ContenedorEJB.jar

    • ClienteEJB.war

servlet代码如下所示。我唯一能做的就是查找sessionBean,

c.lookup("java:global/DemoEJB/ContenedorEJB/BeanStateful!beanssesion.BeanStatefulLocal");

保存它,然后使用实现的方法getValor和setValor来查看我发送给EJB模块的值是否已保存。但它会丢失,因为它找不到BeanStateful,即使它存在。

package servlets;

import beanssesion.BeanStatefulLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletEJB extends HttpServlet {

    BeanStatefulLocal beanStateful = lookupBeanStatefulLocal();

    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 ServletEJB</title>");            
            out.println("</head>");
            out.println("<body>");

            out.println("Enviamos un valor al EJB Stateful: ");
            beanStateful.setValor("Valor desde Servlet");
            out.println("Valor devuelto " + beanStateful.getValor());

            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";
    }// </editor-fold>

    private BeanStatefulLocal lookupBeanStatefulLocal() {
        try {
            Context c = new InitialContext();
            return (BeanStatefulLocal) c.lookup("java:global/DemoEJB/ContenedorEJB/BeanStateful!beanssesion.BeanStatefulLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }   
}

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

当您使用GlassFish 4.1时,如果您将所有类(包括EJB)暂时放在Web模块中,您会发现生活更简单。

其次,您很少需要使用显式JNDI查找;改为使用注射:

public class ServletEJB extends HttpServlet {

    @javax.ejb.EJB
    BeanStatefulLocal beanStateful;

    ...
}

但是,使用像这样的有状态会话bean存在很大问题。此servlet实例将与所有客户端共享,因此您注入/查找的EJB也将共享。这将导致严重的并发问题。