91

what is meant by java:comp/env ?

What does the look up like :

Context envContext = (Context)initContext.lookup("java:comp/env");

do ?

I understand that a look-up like :

(DataSource)envContext.lookup("jdbc/MyDatasource")

looks up for the name MyDatasource in the context.xml or web.xml to get the URL of the database. Is it so ? !! But what does the former look up do ?

4
  • 4
    @Marko Topolnik but that does not answer my question.
    – saplingPro
    Jul 24, 2012 at 13:33
  • 5
    Please open and read the link before voting to close this question. Dont just go after the title.
    – saplingPro
    Jul 24, 2012 at 13:51
  • 1
    It sure looks like an answer to me. It's a root namespace provided by the container for you to look up your resources in.
    – user207421
    Jul 25, 2012 at 3:37
  • This is a good question for people trying to migrate from Weblogic to Tomcat for example
    – ACV
    Jul 24, 2017 at 11:41

3 Answers 3

75

java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB).

Context envContext = (Context)initContext.lookup("java:comp/env");

allows defining a variable pointing directly to this node. It allows doing

SomeBean s = (SomeBean) envContext.lookup("ejb/someBean");
DataSource ds = (DataSource) envContext.lookup("jdbc/dataSource");

rather than

SomeBean s = (SomeBean) initContext.lookup("java:comp/env/ejb/someBean");
DataSource ds = (DataSource) initContext.lookup("java:comp/env/jdbc/dataSource");

Relative paths instead of absolute paths. That's what it's used for.

3
  • What is this node meant for ? There would be many nodes in the JNDI tree.
    – saplingPro
    Jul 24, 2012 at 13:53
  • 12
    I still don't get the feel of what actually is java:comp/env.
    – saplingPro
    Jul 24, 2012 at 13:54
  • 10
    Each JEE component (webapp, EJB) can define properties that are local to this component. And these properties are accessible through java:comp/env. See tomcat.apache.org/tomcat-7.0-doc/… for example. EJBs have the same sort of thing.
    – JB Nizet
    Jul 24, 2012 at 15:02
11

I know I'm far late, but I was asking the same question, and I think I came some answer. So, if I may put my two cents.

java:comp/env/jdbc/myDataSource

  • java: is just like jdbc: from connection string. Acts as a protocol.
  • comp is the root for all JNDI contexts.
  • env is the subcontext for all resource related. There is another for user. Check this out.
  • jdbc is the subcontext for jdbc resources. There are types. Check the link from the previous bullet.
  • myDataSource is the name of your jdbc resource.
3
  • 1
    this should be the accepted answer - clear explanation.
    – likejudo
    Jun 2, 2021 at 16:45
  • @joker: The link you shared from Sun Java System Application Server is a very useful source to understand the jargon in JNDI. I can hardly find it in tomcat docs while setting it as it never explained much there. I found the Admin Guide as well in the Sun link, it clears the whole picture. Thanks.
    – sylye
    Sep 28, 2021 at 8:05
  • 1
    comp is short for "composite", as in javax.naming.CompositeName.
    – gjvc
    Nov 27, 2022 at 20:51
10

It's an in-memory global hashtable where you can store global variables by name.

The "java:" url scheme causes JNDI to look for a javaURLContextFactory class, which is usually provided by your app container, e.g. here is Tomcat's implementation javadoc

See also NamingManager.getURLContext

Not the answer you're looking for? Browse other questions tagged or ask your own question.