What is java: comp / env?

what is meant by java:comp/env ?

What looks like this:

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

do?

I understand that the search is as follows:

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

looks for the name MyDatasource in context.xml or web.xml to get the URL of the database. This is true?!! But what does the first look do?

+53
java java-ee jndi
Jul 24 2018-12-12T00:
source share
2 answers

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

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

allows you to define a variable that points directly to this node. It allows you to do

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

but not

 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. This is what it was used for.

+46
Jul 24 2018-12-12T00:
source share

This is a global hash table in memory in which you can store global variables by name.

The "java:" URL scheme causes JNDI to look for the javaURLContextFactory class, which is usually provided by your application container, for example. here is the implementation of Tomcat javadoc

See also NamingManager.getURLContext

+2
Nov 18 '15 at 3:38
source share



All Articles