Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

ejb - javax.naming.NameNotFoundException: Name [servlet.TestEJB/meuEJB] is not bound in this Context. Unable to find [servlet.TestEJB]

I get an error on this application that keeps users on a BD via a button on the index.html page that calls the servlet by clicking on POST. I would greatly appreciate a solution for a long time I'm trying to find the error causes. I think the error should be the time to put some notes or did not find the 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>");
        }

    }

}

Method:

@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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your stack trace tells you're using Tomcat.

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

Tomcat is not an EJB container. It's a barebones JSP/Servlet container. Throwing in a bunch of JARs in webapp's /WEB-INF/lib just in order to get code to compile doesn't magically turn Tomcat into a true Java EE container.

For EJB, you need a real Java EE container. Replace Tomcat by TomEE, WildFly or Payara. Those are real Java EE containers supporting EJB, JPA, JSF, JAX-RS and all other real stuff out the box.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...