Application Techniques



Using EJB Environment Properties

How to create an environment entry in the EJB deployment descriptor and look it up at runtime.

About this technique

Details

Category

Enterprise JavaBean Techniques

Description

You'll learn about:

Related reading

See the part on developing Enterprise JavaBeans in the Programmer's Guide

Using environment properties   Top of page

Any value that you might want to set that is not a resource reference, a bean reference or a security role, can be set as an environment property. For example, the SilverStream BankDemo uses a String variable called bank_name which allows the deployer to change the name displayed at runtime.

Specifying environment variables in the deployment descriptor   Top of page

To use the JAR Designer to add an environment variable to the deployment descriptor, you add the environment entry and specify its properties as shown here:

The information that you set in the deployment descriptor includes the following:

Property value

Description

Name

The name by which this property will be found.

Value

The value for this property. You can set this to an initial value for testing. The deployer would set this value appropriately at deployment time.

Entry Type

The property's data type.

Description

A textual value that you can use to describe to the deployer what values are appropriate for the property.

At deployment time, the Container adds this entry (and any other environment entries) to the naming context's environment namespace.

Looking up the property at runtime   Top of page

You can find an environment property using the following code to look it up:

  InitialContext m_initialContext = new InitialContext();                  
  m_sBankName = (String)  
  m_initialContext.lookup("java:comp/env/bank_name"); 

or if you need to look up more than one:

  InitialContext m_initialContext = new InitialContext();                  
  Context contextEnv = (Context) m_initialContext.lookup("java:comp/env"); 
  String m_sBankName = (String) contextEnv.lookup("bank_name"); 
   
  // Return the bank name. 
  return m_sBankName; 
   
   





Copyright © 2000, SilverStream Software, Inc. All rights reserved.