Have you tried configuring the JNDI variables with Tomcat server?. This tutorial is going to explain you step-by-step procedure on how to configure JNDI variables in your tomcat server. It is very simple configuration in the Tomcat server’s configuration XML file.
- Open the Tomcat server’s
context.xmlfile. The same change can be done using the
server.xmlfile by adding the configurations inside context element.
- The root element for the
context.xmlfile will be
context. You have to add all the environment entries inside this root element. If you want to add a new JNDI environment entry, please add the new element as below.
<Environment name="envEntryName" value="envEntryValue" type="java.lang.String" override="false" description="Describe about this variable"/>
Here is the explanation for the attributes used for the
- description: It is a human readable description for this variable. This is an optional parameter.
- name : This is the name of the environment entry. This entry is relative to the
- value : This is the value of the parameter returned to the application when accessed from the JNDI context. This value must be convertible to the Java type defined by the
- type : It is fully qualified Java class name expected by the Java web application from this environment entry. This must be a legal value for the
in the web.xml.
- override : Set this value as
falseif you don’t want to add
env-entryfor this entry in the web application deployment descriptor. By default, overrides are allowed.
The above sample example snippet is equivalent of writing the following entries in your web deployment descriptor
<env-entry> <env-entry-name>envEntryName</env-entry-name> <env-entry-value>envEntryValue</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry>
The above entries can be accessed in your Java program as below:
InitialContext initialContext = new javax.naming.InitialContext(); String debug = (String) initialContext.lookup("java:comp/env/envEntryName");
If the JNDI entry is not found, then the above code would throw the javax.naming.NameNotFoundException.