ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzamy_5.4.0.1/50/program/jndicntx.htm

66 lines
2.8 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Obtain the initial JNDI context for the component</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jndicntx"></a>Obtain the initial JNDI context for the component</h5>
<p>To access an enterprise resource such as a data source or JavaMail session in a distributed computing environment, you can use the JNDI API to locate that object so that you can use it in your code. To do this, you must first obtain the initial context that contains the resource (a Java object).</p>
<p>You can obtain an initial JNDI context in one of two ways:</p>
<p>
<ul>
<li><a href="#get">Get an initial context using JNDI properties found in the current environment</a></li>
<li><a href="#set">Get an initial context by explicitly setting JNDI properties</a></li>
</ul>
</p>
<a name="get"></a>
<p><strong>Get an initial context using JNDI properties found in the current environment</strong></p>
<p>The current environment includes the Java system properties and properties defined in properties files found in the classpath. This code snippet illustrates how to obtain the initial context from these properties:</p>
<pre>
import javax.naming.Context;
import javax.naming.InitialContext;
// ... class declaration, method declaration code here ...
Context initialContext = new InitialContext();
</pre>
<a name="set"></a>
<p><strong>Get an initial context by explicitly setting JNDI properties</strong></p>
<p>In general, JNDI clients should assume that the correct environment is already configured. If this is the case, there is no need for you to explicitly set property values and pass them to the constructor of the InitialContext object. However, a JNDI client might need to access a namespace other than the one identified in its environment. In this event, you must explicitly set one or more properties used by the InitialContext constructor. Any property values you pass to the InitialContext constructor take precedence over settings of the equivalent properties found elsewhere in the client's environment.</p>
<p>This code snippet illustrates this technique:</p>
<pre>
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
// ... class declaration, method declaration code here ...
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, &quot;com.ibm.websphere.naming.
WsnInitialContextFactory&quot;);
env.put(Context.PROVIDER_URL, &quot;iiop://myhost.mycompany.com:2809&quot;);
Context initialContext = new InitialContext(env);
</pre>
</body>
</html>