• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Add Environment Entries (JNDI) to Tomcat Web Server

March 2, 2016 //  by Krishna Srinivasan//  Leave a Comment

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.

Tomcat JNDI Configuration
Tomcat JNDI Configuration
  1. Open the Tomcat server’s context.xml file. The same change can be done using the server.xml file by adding the configurations inside context element.
  2. The root element for the context.xml file 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 Environment element:

  • 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 java:comp/env context.
  • 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 attribute.
  • 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 false if you don’t want to add env-entry for 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 web.xml:

<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.

Category: ServersTag: Apache Tomcat

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Maven : updatePolicy Configuration in Settings XML file
Next Post: Array Vs ArrayList in Java java»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact