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


Description


<env-entry-name>


Specifies the environment name to which the value is bound.


<env-entry-type>


Specifies the environment variable type as one of the following:


java.lang.Boolean


java.lang.Byte


java.lang.Double


java.lang.Float


java.lang.Integer


java.lang.Long


java.lang.String


<env-entry-value>


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:


<env-entry>


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


Custom tags and J2EE


<env-entry-type>java.lang.String</env-entry-type>


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


</env-entry>


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.


System.out.println(initContext.lookup(«java:comp/env/sampleValue»));


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»