问题
Where and how should the EJB ConverterBean
be located so that ConverterServlet
can inject the EJB? Does not:
"..with Java EE 6 and newer you can just put all the classes in your WAR file"
I'm reading the Java EE 7 tutorial on this topic, of course.
the InjectionException
from the browser:
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 net.bounceme.dur.servlets.ConverterServlet
root cause
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class net.bounceme.dur.servlets.ConverterServlet
root cause
java.lang.IllegalStateException: Exception attempting to inject Remote ejb-ref name=net.bounceme.dur.servlets.ConverterServlet/converter,Remote 3.x interface =net.bounceme.dur.ejb.ConverterBean,ejb-link=null,lookup=,mappedName=,jndi-name=net.bounceme.dur.ejb.ConverterBean,refType=Session into class net.bounceme.dur.servlets.ConverterServlet: Lookup failed for 'java:comp/env/net.bounceme.dur.servlets.ConverterServlet/converter' 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
com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=net.bounceme.dur.servlets.ConverterServlet/converter,Remote 3.x interface =net.bounceme.dur.ejb.ConverterBean,ejb-link=null,lookup=,mappedName=,jndi-name=net.bounceme.dur.ejb.ConverterBean,refType=Session into class net.bounceme.dur.servlets.ConverterServlet: Lookup failed for 'java:comp/env/net.bounceme.dur.servlets.ConverterServlet/converter' 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/net.bounceme.dur.servlets.ConverterServlet/converter' 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=net.bounceme.dur.servlets.ConverterServlet/converter,Remote 3.x interface =net.bounceme.dur.ejb.ConverterBean,ejb-link=null,lookup=,mappedName=,jndi-name=net.bounceme.dur.ejb.ConverterBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean' [Root exception is javax.naming.NamingException: Lookup failed for 'net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean' 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: net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean not found]]]
root cause
javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=net.bounceme.dur.servlets.ConverterServlet/converter,Remote 3.x interface =net.bounceme.dur.ejb.ConverterBean,ejb-link=null,lookup=,mappedName=,jndi-name=net.bounceme.dur.ejb.ConverterBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean' [Root exception is javax.naming.NamingException: Lookup failed for 'net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean' 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: net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean not found]]
root cause
javax.naming.NamingException: Lookup failed for 'net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean' 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: net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean not found]
root cause
javax.naming.NameNotFoundException: net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean 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.1 logs.
GlassFish Server Open Source Edition 4.1.1
Excerpt from tail -f
of the Glassfish log from deployment to NameNotFoundException
:
[2017-04-18T23:23:53.687-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033687] [levelValue: 800] [[
visiting unvisited references]]
[2017-04-18T23:23:53.709-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033709] [levelValue: 800] [[
visiting unvisited references]]
[2017-04-18T23:23:53.719-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033719] [levelValue: 800] [[
visiting unvisited references]]
[2017-04-18T23:23:53.725-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.tools.deployment.common] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033725] [levelValue: 800] [[
visiting unvisited references]]
[2017-04-18T23:23:53.757-0700] [glassfish 4.1] [INFO] [AS-EJB-00054] [javax.enterprise.ejb.container] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033757] [levelValue: 800] [[
Portable JNDI names for EJB ConverterBean: [java:global/ConverterWebAppicationArchiveWithEJB/ConverterBean, java:global/ConverterWebAppicationArchiveWithEJB/ConverterBean!net.bounceme.dur.ejb.ConverterBeanLocal]]]
[2017-04-18T23:23:53.787-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033787] [levelValue: 900] [[
WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]
[2017-04-18T23:23:53.795-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033795] [levelValue: 900] [[
WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]
[2017-04-18T23:23:53.799-0700] [glassfish 4.1] [WARN] [] [org.jboss.weld.Event] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033799] [levelValue: 900] [[
WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.]]
[2017-04-18T23:23:53.966-0700] [glassfish 4.1] [INFO] [AS-WEB-GLUE-00172] [javax.enterprise.web] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033966] [levelValue: 800] [[
Loading application [ConverterWebAppicationArchiveWithEJB] at [/ConverterWebAppicationArchiveWithEJB]]]
[2017-04-18T23:23:53.976-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.system.core] [tid: _ThreadID=154 _ThreadName=admin-listener(8)] [timeMillis: 1492583033976] [levelValue: 800] [[
ConverterWebAppicationArchiveWithEJB was successfully deployed in 294 milliseconds.]]
[2017-04-18T23:23:54.263-0700] [glassfish 4.1] [INFO] [] [javax.enterprise.web] [tid: _ThreadID=95 _ThreadName=http-listener-1(3)] [timeMillis: 1492583034263] [levelValue: 800] [[
WebModule[null] ServletContext.log():Marking servlet net.bounceme.dur.servlets.ConverterServlet as unavailable]]
[2017-04-18T23:23:54.272-0700] [glassfish 4.1] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=95 _ThreadName=http-listener-1(3)] [timeMillis: 1492583034272] [levelValue: 900] [[
StandardWrapperValve[net.bounceme.dur.servlets.ConverterServlet]: Allocate exception for servlet net.bounceme.dur.servlets.ConverterServlet
javax.naming.NameNotFoundException: net.bounceme.dur.ejb.ConverterBean#net.bounceme.dur.ejb.ConverterBean not found
at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:237)
at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:204)
at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:66)
at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:114)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:478)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:183)
at com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl$EjbReferenceProxy.create(ComponentEnvManagerImpl.java:1015)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:745)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:715)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:159)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:471)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:636)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:507)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:170)
at org.glassfish.weld.services.InjectionServicesImpl.aroundInject(InjectionServicesImpl.java:165)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:46)
at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72)
at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121)
at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:336)
at org.glassfish.weld.services.JCDIServiceImpl.createManagedObject(JCDIServiceImpl.java:263)
at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:485)
at com.sun.enterprise.container.common.impl.managedbean.ManagedBeanManagerImpl.createManagedBean(ManagedBeanManagerImpl.java:439)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:336)
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:991)
at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:2130)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1404)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1211)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:237)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
]]
servlet trying to inject ConverterBean
:
package net.bounceme.dur.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.bounceme.dur.ejb.ConverterBean;
@WebServlet("/converter")
public class ConverterServlet extends HttpServlet {
@EJB
ConverterBean converter;
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
// converter = new ConverterBean();
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ConverterServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("what is the amount?");
out.println("<h1>Servlet ConverterServlet at " + request.getContextPath() + "</h1>");
String stringAmount = "999";
out.println("amount is " + stringAmount);
out.println("<p>");
BigDecimal dollars = new BigDecimal(stringAmount);
out.println("dollars:\t" + dollars + "\t" + dollars.getClass());
out.println("<p>");
// BigDecimal yen = converter.dollarToYen(dollars);
// BigDecimal euros = converter.yenToEuro(yen);
out.println("never executed..");
out.println("yen");
// out.println(yen);
out.println("<p>");
out.println("euros");
// out.println(euros);
out.println("<p>");
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
the tree
structure of the project:
.
├── build.xml
├── nbproject
│ ├── ant-deploy.xml
│ ├── build-impl.xml
│ ├── genfiles.properties
│ ├── private
│ │ └── private.properties
│ ├── project.properties
│ └── project.xml
├── src
│ ├── conf
│ │ └── MANIFEST.MF
│ └── java
│ └── net
│ └── bounceme
│ └── dur
│ ├── ejb
│ │ ├── ConverterBean.java
│ │ ├── ConverterBeanLocal.java
│ │ └── PropertiesReader.java
│ └── servlets
│ └── ConverterServlet.java
├── test
└── web
├── index.html
└── WEB-INF
└── web.xml
13 directories, 14 files
I tried putting the servlet
and ConverterBean
in the same package, also.
回答1:
You seem to have defined a local interface for ConverterBean
called ConverterBeanLocal
.
Therefore you will need to use that at the injection point:
@WebServlet("/converter")
public class ConverterServlet extends HttpServlet {
@EJB
ConverterBeanLocal converter;
Alternatively, you could get rid of the ConverterBeanLocal
interface altogether and make your code simpler.
来源:https://stackoverflow.com/questions/43488182/glassfish-error-namenotfoundexception-so-servlet-cannot-inject-ejb