jQuery in Action, 3rd edition
The moose likes EJB and other Java EE Technologies and the fly likes How to do JNDI lookup? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How to do JNDI lookup?" Watch "How to do JNDI lookup?" New topic

How to do JNDI lookup?

Anuj Singh
Ranch Hand

Joined: May 04, 2007
Posts: 47
I am having a JNDI named as jdbc/raDataSource.Now how to do the JNDI lookup??HOw will my application get the datasource which I have created?Also,what does the following mean: "java:comp/env/jdbc/raDataSource"??Where&in which class of my app will I write the code for the JNDI lookup?

SJCP 1.5 with 93%.Preparing for WCD
Vijitha Kumara

Joined: Mar 24, 2008
Posts: 3902

To do a JNDI lookup you have to aquire the naming context and use that to access any resource.


[How to ask questions] [Twitter]
Anuj Singh
Ranch Hand

Joined: May 04, 2007
Posts: 47
Thanks Vijitha for the reply.What does this mean:java:comp/env?Anyways..moreover now how will I tell the webapp abt the datasource?Is it in the web.xml smthg like dis:



Also,in which part of my code will I write the code for JNDI lookup?

Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3915

The java:comp/.... means the JNDI name for a particular resource, as for here, it is used for the data source. And your XML structure is correct for configuring a datasource to be used with JNDI, but that is 'not' to be used in web.xml descriptor. You need a separate descriptor like ejb-jar.xml (which will be deployed in the EJB container) to use this XML structure. However, with EJB 3.0, you don't need to use these XML configuration or even looking up for JNDI, because you can use annotations instead.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ray Blue

Joined: Jul 12, 2009
Posts: 8
does comp/env also work with windows?? my app ran under jboss. and in glassfish i managed to get it deployed too. even my stateless session beans work. but i am not allowed to use DI for stateful session beans. so I would have to use lookup. But lookup works different in Glassfish than in JBoss Some "full" example would be great. I already added some ConnectionPool successfuly and some jdbc/mydb connection resource. The only thing that i think is missing is the correct JNDI lookup

SCJP 6.0
I agree. Here's the link: http://aspose.com/file-tools
subject: How to do JNDI lookup?
It's not a secret anymore!