Pages

Monday, September 27, 2010

Java Naming and Directory Interface (JNDI) with Environment Variables

The environment allows you to define your variables name, value and type. At runtime the environment values are not allowed to modify.

Declared the environment variables using the <env-entry> tag in the deployment descriptor (WEB-INF/web.xml in Tomcat). The elements of the <env-entry> tag are:
* <description>: an optional description.
* <env-entry-name>: the entry name.
* <env-entry-value>: a value.
* <env-entry-type>: the java variable type(Boolean, Byte, Double, Character, Float, Integer, Long, Short, String).

Example
<env-entry>
  <description>DNS Database Connection DriverName</description>
  <env-entry-name>driver</env-entry-name>
  <env-entry-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</env-entry-value>
  <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

<env-entry>
  <description>DNS Pagination of home products</description>
  <env-entry-name>page/home</env-entry-name>
  <env-entry-value>16</env-entry-value>
  <env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
Each tag describes a single environment entry. In this example, two environment entries have been defined.

Code shows to accesses the environment entries
import java.io.*;
import javax.naming.*;
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
String DRIVER = (String) envCtx.lookup("driver");
Integer PAGINATION_HOME= (Integer) envCtx.lookup("page/home");
}
catch (Exception e){)

No comments:

Post a Comment