It's all about the answers!

Ask a question

how to use jndi


wang weimin (11) | asked Aug 11 '09, 10:51 a.m.
I write a simple servlet as following.

public class HelloWorld2 extends HttpServlet {


public HelloWorld2() {
// TODO Auto-generated constructor stub
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().println("<html><body>Hello World</body></html>");
try {
Properties prop = new Properties();
//prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.mortbay.jndi.InitialContextFactory");
//prop.put(Context.URL_PKG_PREFIXES, "org.mortbay.naming");
//org.apache.naming.java.javaURLContextFactory d = new org.apache.naming.java.javaURLContextFactory();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
prop.put(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext(prop);
DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/DSTest");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}


and I configured this servlet in plugin.xml


<extension>
<servlet></servlet>
</extension>


but when I access this servlet with url http://localhost:9080/taas/helloworld2

I got the following error:


javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable {java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory, java.naming.factory.url.pkgs=org.apache.naming} [Root exception is java.lang.NullPointerException]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:238)
at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:322)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:352)
at javax.naming.InitialContext.internalInit(InitialContext.java:290)
at javax.naming.InitialContext.<init>(InitialContext.java:207)
at com.ibm.taas.core.web.HelloWorld2.service(HelloWorld2.java:31)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.eclipse.equinox.http.registry.internal.ServletManager$ServletWrapper.service(ServletManager.java:180)
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleRequest(ServletRegistration.java:90)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(ProxyServlet.java:111)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:59)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:428)
at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:473)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:568)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:633)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:820)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:986)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:837)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:245)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: java.lang.NullPointerException
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:230)
... 24 more


someone can give me sample on how to use JNDI on jazz

One answer



permanent link
Jose Miguel Ordax Cassa (2.4k3126100) | answered Apr 18 '13, 7:17 a.m.
No positive or negative comments since 2009? :-(

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.