Using Servlet Parameters
A servlet may receive initialization parameters from the web.xml file, as shown in Listing 3.
Commons Configuration also provides a solution for obtaining these parameters:
public void init(ServletConfig config) {
Configuration cont = new ServletConfiguration(config);
listProperties(cont);
}
Below, the listProperties method shows the configuration data received:
*** Contents of configuration object:
org.apache.commons.configuration.web.ServletConfiguration
password=secret
userid=Peter
A Commons Configuration class can even access the request parameters:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Configuration configuration = new ServletRequestConfiguration(req);
. . .
Click here for a look at all the implementations of the Configuration interface.
How to Combine Configurations
If parameters are obtained from disparate sources, you may still want to expose them as coming from one source. These are called "composite configurations":
. . .
Configuration config = new PropertiesConfiguration
("myfirsttry.properties");
Configuration systemConfig = new SystemConfiguration();
CompositeConfiguration composite = new CompositeConfiguration();
composite.addConfiguration(systemConfig);
composite.addConfiguration(config);
. . .
The above example combines system properties with properties from a properties file. If a property is found in both sources, the system property wins, because it was placed first. However, you may also enter a system property from the command line, so this setup gives you a way to override parameters from the properties file.
Suppose your properties file looks like this:
day = 13
age = 62
ages = 12, 13, 21, ${age}
os.name = Kelds Operating System
If you obtain the parameter day from the composite object shown above, the result is the value 13. But if the Java program starts like this:
java -Dday=5 hansen.playground.MyComposite
the result is value 5. This offers an easy way to override configuration parameters.
An alternative to defining the configuration sources in your program is to define them in a configuration file. Here's an XML file that defines the same set of configuration sources as above:
<configuration>
<system/>
<properties fileName="myfirsttry.properties"/>
</configuration>
This configuration is then read from a program like this:
. . .
ConfigurationFactory factory = new ConfigurationFactory("config.xml");
Configuration config = factory.getConfiguration();
. . .
Common Configuration provides for many additional use possibilities and, luckily, the documentation on all features is stellar.
Dependencies
Depending on which features from Commons Configuration you want to use, you'll need to have several .jar files (from the Apache Commons project) available. Click here for details.
Possibilities and Options!
Commons Configuration offers a uniform interface with which to acquire configuration parameters. An old and important design rule is: "hide implementation details," and this is exactly what Commons Configuration allows you to do. You can select the source of parameters that suits you best. Some parameters are best stored in a properties file, but if you want to exchange a file with another system, storing it in an XML file might be your best option. Saving parameters in a database is a possible storage solution for parameters you want to update from your application. Further, Commons Configuration allows you to define the priority of the sourcesin a configuration file, of course.
Happy coding!
Related Resources
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.