Metawerx Java Hosting Small Logo

<env-entry>

This tag provides environment variables (properties) which can be accessed by your web application using JNDI.

See advantages of specifying parameter values in web.xml for reasons you would want to use this tag.

Env entries are similar to providing init-params for a servlet or filter, or context-params for your application, except they use JNDI.

A single <env-entry> tag is used for each property.

The property name and value are set using <env-entry-name> and <env-entry-value>. <env-entry-type> is used to specify the datatype, which is usually java.lang.String. This can be any fully qualified class name, as long as the class has a constructor that accepts a single string.

The following example is an entry named "webmasterEmail". This might be used to send administrative alerts, such as error dumps from a custom error page.

<env-entry> 
    <env-entry-name>webmasterEmail</env-entry-name> 
    <env-entry-type>java.lang.String</env-entry-type> 
    <env-entry-value>admin@domain.com</env-entry-value> 
</env-entry>

To access the properties from your Java/JSP code, use the following syntax:

// Get a handle to the JNDI environment naming context
Context env = (Context)new InitialContext().lookup("java:comp/env");

// Get a single value
String webmasterEmail = (String)env.lookup("webmasterEmail");

As with all JNDI names, the <env-entry-name> can be a path instead of a single name. For example:

<env-entry-name>defaults/alerts/webmasterEmail</env-entry-name> 

Simple JNDI in web.xml vs. Complex entries in context.xml and server.xml files

  • The <env-entry> tag in web.xml only allows simple data types to be defined, which can be entered as a single string. These types are Boolean, String, Integer, Double and Float.
  • For more complex JNDI data, the context.xml or server.xml file can be used. This allows for definition of objects such as JNDI DBCP data sources.
  • An example of where you might want to use both types, is if you have multiple data sources defined in context.xml, for example, a local datasource called jdbc/datalocal, a deployment datasource called jdbc/datalive, and a staging datasource called jdbc/datastaging. Your web.xml file can then contain an <env-entry> environment variable, which points to the JNDI URL of the active datasource. Changing between the different datasources is then as simple as changing the environment variable in web.xml.

More Information

  • Using J2EE Services from JSP from Sun, contains Environment Variable, EJB Reference and Resource Reference examples
  • JNDI - this page has a lot more information on using JNDI, and how data can be specified in other areas than web.xml
navigation
metawerx specific
search
Share
tools
help

referring pages
Valid XHTML 1.0 Transitional

Share