JNDI Context

Like JDBC (Java Database Connectivity), JNDI is not a service, but a set of interfaces that allows applications to access many different directory service providers using a standardized API.

JNDI Context

JNDI Context 

     

Like JDBC (Java Database Connectivity), JNDI is not a service, but a set of interfaces that allows applications to access many different directory service providers using a standardized API. The deployed enterprise beans in EJB have an environment-naming context that can be accessed using the JNDI API. It also accesses an enterprise resource such as a data source or JavaMail session in a distributed computing environment.

 To do this, you must first obtain the initial context that contains the resource (a Java object).

The java.naming.Context interface provides methods for retrieving and updating this name-to-object binding environment in which each name passed as an argument to a Context method is relative to that context. A name parameter may never be null.

 

Storing and retrieving objects from a JNDI namespace is quite straightforward. You first obtain a JNDI naming context and then use the bind() and lookup() methods to store and retrieve objects, as shown below:


 Store and retrieve objects from a default JNDI namespace

import javax.naming.*;
public void createName() throws NamingException {
Context context = new InitialContext();
context.bind("java:comp/env", "MyApp");
}
public String getName() throws NamingException {
Context context = new InitialContext();
return (String) context.lookup("java:comp/env");
}


Read more at:

http:/www.roseindia.net/software-tutorials/detail/3658