Load Configuration Files from the Classpath
From Servlet API 1.0 through Servlet API 2.3, servlets have distinctly lacked a standard mechanism to retrieve external configuration files. Although many server-side libraries require configuration files, servlets have no commonly accepted way to locate them. When a servlet runs under J2EE, it receives support for JNDI, which can provide a certain amount of configuration information. But the common web server configuration file problem remains.
The best solution (or perhaps I should call it the “lesser evil” solution) is to locate files with a search of the classpath and/or the resource path. This lets server admins place server-wide configuration files in the web server’s classpath, or place per-application configuration files in WEB-INF/classes found in the resource path. It also works equally well for locating configuration files placed within WAR files and/or deployed across multiple back-end servlet containers. In fact, using files for configuration has several advantages, even when JNDI is available. The component provider can include a set of “sample” or “default” configuration files. One configuration file can be made to work across the entire server. And finally, configuration files are trivially easy to understand for both the developer and deployer.
Example 3-4 demonstrates the search technique with a
Given a resource name, the
Resource constructor searches the class path and resource path attempting ...