Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is java:comp/env? [duplicate]

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 ?

like image 428
saplingPro Avatar asked Jul 24 '12 13:07

saplingPro


People also ask

What is java comp env?

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");

What is lookup in JNDI?

The Java RMI and Java EE APIs use the JNDI API to look up objects in a network. The API provides: a mechanism to bind an object to a name. a directory-lookup interface that allows general queries. an event interface that allows clients to determine when directory entries have been modified.


3 Answers

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.

like image 175
JB Nizet Avatar answered Oct 22 '22 21:10

JB Nizet


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

like image 23
Rich Avatar answered Oct 22 '22 21:10

Rich


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.
like image 44
joker Avatar answered Oct 22 '22 23:10

joker