javax.naming.NameNotFoundException:名称[servlet.TestEJB / meuEJB]未绑定在此Context中。无法找到[servlet.TestEJB]

时间:2015-12-29 20:22:06

标签: servlets ejb

我在此应用程序上遇到错误,该错误通过index.html页面上的按钮将用户保留在BD上,该页面通过单击POST调用servlet。 我非常感谢解决方案很长一段时间我试图找到错误原因。 我认为错误应该是放一些笔记或找不到servlet的时候。

ERROR:

javax.servlet.ServletException: Error instantiating servlet class servlet.TestEJB
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

javax.naming.NameNotFoundException: Name [servlet.TestEJB/meuEJB] is not bound in this Context. Unable to find [servlet.TestEJB].
    org.apache.naming.NamingContext.lookup(NamingContext.java:818)
    org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

SERVLET:

@WebServlet("/TestEJB")
public class TestEJB extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @EJB
    MissatgesEJB meuEJB;

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");

        try (PrintWriter out = resp.getWriter()) {

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet TestEJB</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Proves Whatsapp</h1>");

            Usuaris usuari = new Usuaris("robert_1994", "Paucasals33", "Bruch");

            out.println("Insertant Usuari... " + meuEJB.insertarUsuari(usuari) + "<br/>");

            out.println("</body>");
            out.println("</html>");
        }

    }

}

方法:

@Stateless
public class MissatgesEJB {

    @PersistenceUnit
    EntityManagerFactory emf;

    public String insertarUsuari(Usuaris u) {

        EntityManager em = emf.createEntityManager();

        try {
            // Excriu a la BD.
            em.persist(u);

            // Forçem a que escrigui ara ala BD.
            em.flush();

            return "ok";

        } catch (Exception ex) {

            return ex.getMessage();

        } finally {

            em.close();
        }


    }

}

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="JPA-1" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>beans.Historial</class>
        <class>beans.Missatges</class>
        <class>beans.Usuaris</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/bdwhatsapp" />
            <property name="javax.persistence.jdbc.user" value="marti" />
            <property name="javax.persistence.jdbc.password" value="Paucasals33" />
            <property name="javax.persistence.schema-generation.database.action"
                value="none" />
        </properties>
    </persistence-unit>
</persistence>

1 个答案:

答案 0 :(得分:3)

您的堆栈跟踪告诉您正在使用Tomcat。

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

Tomcat不是EJB容器。它是一个准系统的JSP / Servlet容器。为了获得编译代码,在webapp的/WEB-INF/lib中抛出一堆JAR并不会让Tomcat神奇地变成真正的Java EE容器。

对于EJB,您需要一个真正的Java EE容器。按TomEEWildFlyPayara替换Tomcat。这些是真正的Java EE容器,支持EJB,JPA,JSF,JAX-RS和所有其他真正的东西。

另见: