InfoCenter Home >
4: Developing applications >
4.6: Java Technologies >
4.6.2: JNDI (Java Naming and Directory Interface) overview >
4.6.2.2: Using Java Naming Directory Interface
4.6.2.2: Using Java Naming Directory Interface
If you want to use the Java Naming and Directory Interface (JNDI) client on WebSphere Application Server Version 3.5.3 or
Version 3.5.4 to access a Version 4.0.x name server, you must apply fix PQ51387
to your Version 3.5.x product. This fix is available at
http://www.ibm.com/software/webservers/appserv/support.html.
Follow the instructions in the README to update the ujc.jar file and ns.jar file
on WebSphere Application Server Version 3.5.x.
Refer to these examples to learn how to use JNDI.
Get an initial context
In general, JNDI clients should assume the correct environment is
already configured so there is no need to explicitly set property values and pass
them to the InitialContext constructor. However, a JNDI client may need
to access a name space other than the one identified in its environment.
In this event, it is necessary to
explicitly set one or more properties used by the InitialContext constructor. Any
property values passed in directly to the InitialContext constructor take precedence
over settings of those same properties found elsewhere in the environment.
View the following examples for information on passing property values to the
InitialContext constructor:
- Get an initial context using JNDI properties found in the current environment:
The current environment includes the Java system properties and properties defined in
properties files found in the JNDI client's CLASSPATH. See article Installing files and setting classpaths
for information on defining CLASSPATHs.
...
import javax.naming.Context;
import javax.naming.InitialContext;
...
Context initialContext = new InitialContext();
...
- Get an initial context by explicitly setting JNDI properties:
...
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
env.put(Context.PROVIDER_URL, "iiop://myhost.mycompany.com:900");
Context initialContext = new InitialContext(env);
...
- Look up a home for an EJB
The example below shows a lookup of an EJB home. The actual home lookup name
is determined by the application's deployment descriptors.
// Get the initial context as shown in the previous example
...
// Look up the home interface using the JNDI name
try {
java.lang.Object ejbHome = initialContext.lookup("java:comp/env/comp/mycompany/accounting");
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(
(org.omg.CORBA.Object) ejbHome, AccountHome.class);
}
catch (NamingException e) { // Error getting the home interface
...
}
- Look up a JavaMail session:
The example below shows a lookup of a JavaMail resource. The actual lookup name
is determined by the application's deployment descriptors.
// Get the initial context as shown above
...
Session session = (Session) initialContext.lookup("java:comp/env/mail/MailSession");
|
|