JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»
12.3.3 Setting environment entries

First we look at J2EE service’s configuration for environment variables. To do this, we need to modify the web application deployment descriptor (web.xml, which we first saw in chapter 2) by making an entry for each variable. Specifically, in the deployment descriptor, we encapsulate an environment variable within an <env-entry> tag. Like most entries in the deployment descriptor, you can assign it an optional description in addition to several mandatory values (table 12.3).

Table 12.3 Important environment variable information

Elements of the <env-entry> tag



Specifies the environment name to which the value is bound.


Specifies the environment variable type as one of the following:









Specifies an optional property value that must match the type supplied within the <env-entry-type>. If the value is not specified within the deployment descriptor, one must be specified during the deployment.

Next is a fragment taken from a descriptor that creates a string environment variable with a predefined value:


<description> Sample environment value</description> <env-entry-name>sampleValue</env-entry-name>

Custom tags and J2EE


<env-entry-value>This is a sample String</env-entry-value>


With the above entry in our web.xml file, let’s see how the code for gaining access to sampleValuelooks:

// Getting a naming initial context.

javax.naming.Context initContext = new javax.naming.InitialContext();

// Printing the environment value.


Getting the value requires using some of the JNDI APIs. First you need to obtain the default naming context by instantiating a new Java naming context, which provides a handle into the directory in which the services and environment variables are listed. With the directory context in hand, you simply look up the environment value by its JNDI URL, in this case, «java:comp/env/sampleValue».

Скачать в pdf «JSP Tag Libraries»