I just got my hands on doing dynamic web programming using JSP. What is the proper way to handle the configurations?
For example, database name, host, login, and password, and indexing directory in the server, etc. My concern is mostly about the security of the passwords. Currently I hard code the data into the .java files, I don’t think this is the right way to do so I would like to learn from your experiences.
Configuration is usually stored in a properties or XML file which is been placed in the application’s runtime classpath or at a fixed location which is specified as a VM argument. A properties file can be accessed using
java.util.PropertiesAPI. A XML file can be parsed using JAXP or JAXB.Here’s an example of such a properties file:
Assuming that it’s named
config.propertiesand it’s been placed in the root of the classpath (or its root path is been added to the classpath), here’s how you could load it from the classpath:Here’s an example of a XML file:
Assuming that it’s called
config.xmland it’s been placed in the root of the classpath, here’s an example how you could load it by JAXP:It’s only a bit more verbose although JAXB can make life easier if it’s a rather complex file.
Securing the access to properties or XML files in turn is to be controlled at higher (OS/platform) level.
See also: