2

When you develop a webapp with a scripting language you have a file in your project's directory tree where configurations are defined at deploy time and read for initialization at runtime. Things change significantly when you deploy your webapp as a compiled WAR file, because you neither have access to any directory tree nor typically edit the content of the archive.

This is my scenario: my app needs smtp.properties for sending emails at runtime, so this file contains somehow sensitive information which I don't want to share with other webapps. This means that I won't put the file in the $CLASSPATH. Another option is checking in a known location, but this has obvious portability issues. Just to name one: on Linux you have /etc, on Windows there is no /etc

Currently I read the settings with ServletContext.getInitParameter(String), but this impose a constraint on the data format and the actual file's place depends on the servlet container.

So my final answer is: what is the closest match to config.php (or config.yml) in JavaEE's land?

Disclaimer: I read lots of similar question, and it seems that the short answer is no way. This is also cumbersome when you wish to override the settings for other components at deploy time (like logging)

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • This will be tough to do in an application server and platform agnostic way. The most generic way to do this would probably be via a system property defining the location of environment specific configuration files. One extra step for the deployer but its platform agnostic and would survive both application redeployments and application server upgrades. – Perception Feb 25 '12 at 13:26
  • This was also my favorite approach, but isn't straight as a `config.php` – Raffaele Feb 25 '12 at 18:01

2 Answers2

1

In the Java EE world the way to go is per JNDI. Inside your web application you can read the configuration settings via the standardized JNDI API. This includes something like login credentials for SMTP but also configured database connections for example.

For details read my answer for a similiar question: How to portably read configuration data from a servlet

Community
  • 1
  • 1
vanje
  • 10,180
  • 2
  • 31
  • 47
  • This is a clever answer and I already looked at your previous post before asking my question. The problem I'm concerned with is security (since I never played with JNDI): this way every webapp will share the same configuration (for example for DB and mail). Also, I don't exactly understand the advantage of using another API when you already have servlet init params. Can you outline this? – Raffaele Feb 25 '12 at 19:13
0

In Java the equivalent of a config.php file is definitively the web.xml file in WEB-INF. I don't see the problem with the actual file's place depends on the servlet container, you never specify a path to read parameters within, no?

By the way if you want to make your own property file in the war you can acces it using :

URL propPath = getClass().getResource("your_relative_path");
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • The beauty of `config.php` is that it can be manually edited at deploy time, while `WEB-INF/web.xml` is not accessible without exploding (then repacking) the WAR. Also, it must be XML. With Tomcat one can use [this](http://stackoverflow.com/a/1626190/315306) – Raffaele Feb 25 '12 at 11:30
  • Yes if you want to edit in the War it will be harder since you have to repack the war (and probably refresh the web.xml, I don't know how...) – alain.janinm Feb 25 '12 at 11:56