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

77 lines
3.1 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>Use JNDI to look up Java components</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><a name="jndilkup"></a>Use JNDI to look up Java components</h5>
<p>To access some Java objects (such as data sources, or JavaMail sessions) in a distributed computing environment, you can use the JNDI API. These Java objects are sometimes referred to as <strong>enterprise resources</strong>.</p>
<p>Before you can access an enterprise resource in a JNDI environment, you must first <a href="jndicntx.htm">obtain the initial JNDI context for the component</a>. The examples that follow assume that you have already written code to obtain the initial context, which is represented by the object name initialContext:</p>
<p>
<ul>
<!--<li><a href="#EnterpriseBean">Example: Look up an enterprise bean</a></li>-->
<li><a href="#DataSource">Example: Look up a data source</a></li>
<li><a href="#JavaMail">Example: Look up a JavaMail session</a></li>
</ul>
</p>
<!--<a name="EnterpriseBean"></a>
<p><strong>Example: Look up an enterprise bean</strong></p>
<p>The code snippet that follows shows a lookup of an enterprise bean's home interface. The actual lookup name of this interface is determined by the application's deployment descriptors.</p>
<pre>
AccountHome accountHome = null;
try {
java.lang.Object ejbHome = initialContext.lookup(&quot;java:comp/env/ejb/accounting&quot;);
accountHome = (AccountHome)javax.rmi.PortableRemoteObject.narrow(ejbHome, AccountHome.class);
}
catch (NamingException e) {
// Error getting the home interface
// ... error-handling code ...
}
</pre>-->
<a name="DataSource"></a>
<p><strong>Example: Look up a data source</strong></p>
<p>The DataSource object is defined in the JDBC 2.0 Optional Package. The actual name to lookup is defined by the JNDI Name property for the DataSource object. You can use the Websphere administrative console to view this property. For example, if you have a DataSource object named AccountDataSource, the JNDI name for that DataSource would be java:comp/env/jdbc/AccountDataSource.</p>
<pre>
try {
DataSource ds = (DataSource)initialContext.lookup(&quot;java:comp/env/jdbc/AccountDataSource&quot;);
}
catch (NamingException e) {
// Error getting the data source object
// ... error-handling code ...
}
</pre>
<a name="JavaMail"></a>
<p><strong>Example: Look up a JavaMail session</strong></p>
<p>The actual name to look up is defined in the deployment descriptor of the Web application that contains your servlets or JSP files that use the JavaMail API.</p>
<pre>
try {
Session session = (Session)initialContext.lookup(&quot;java:comp/env/mail/MailSession&quot;)
}
catch (NamingException e) {
// Error getting the mail session
// ... error-handling code ...
}
</pre>
</body>
</html>