Cannot acquire InitialContext from a Servlet
ila1905 Mar 19, 2013 1:42 PMI have the following servlet wich arises a NamingException when calling the instruction InitialContext ctx = new InitialContext(jndiProps);
package ejbfrompojoweb;
import java.io.IOException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 ejbfrompojo.EjbInheritedFromPojoMath;
@WebServlet("/EjbFromPojo")
public class EjbInheritedFromPojoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EjbInheritedFromPojoMath ejbMath = null;
try {
Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put("java.naming.provider.url","remote://localhost:4447");
jndiProps.put("jboss.naming.client.ejb.context", true);
jndiProps.put(Context.SECURITY_PRINCIPAL,"userid");
jndiProps.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext ctx = new InitialContext(jndiProps);
ejbMath = (EjbInheritedFromPojoMath)ctx.lookup(
"EJBInheritedFromPOJO_JB/EjbInheritedFromPojoBean!ejbfrompojo.EjbInheritedFromPojoMath");
} catch (NamingException e) {
e.printStackTrace();
}
request.setAttribute("ejbMath", ejbMath);
request.getRequestDispatcher("/EJBFunctionsResults.jsp").forward(request,response);
}
}
This is the exeption log:
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.EJBInheritedFromPojoWEB_JB.war:main" from Service Module Loader
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:64)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.naming.InitialContext.init(InitialContext.java:242)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.naming.InitialContext.<init>(InitialContext.java:216)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at ejbfrompojoweb.EjbInheritedFromPojoServlet.doGet(EjbInheritedFromPojoServlet.java:43)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
18:34:32,158 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
18:34:32,174 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) at java.lang.Thread.run(Thread.java:722)
The same code executed from a standalone client works fine.
I tried to see a lot of Internet sites to find a solution, but uselessly.
I am working with JBoss AS 7 in Eclipse IDE.
Can someone help me? Thank you very much