Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you read a java property in web.xml?

I would like to control the settings in web.xml and using different once for different environments.

Is it possible to use a property, from a property file on classpath, in web.xml? Something like this:

 <context-param>
  <param-name>myparam</param-name>
  <param-value>classpath:mypropertyfile.properties['myproperty']</param-value>
 </context-param>

Best regards

P

like image 430
per_jansson Avatar asked Jun 01 '10 10:06

per_jansson


People also ask

How read properties file in Java application?

Each key and its corresponding value in the property list is a string. The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties. load() method of Properties class is convenient to load .

What type of file is the properties file in Java?

properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.


3 Answers

No. However you can pass the properties file in and read from it at runtime.

<context-param>
    <param-name>propfile</param-name>
    <param-value>myprop.properties</param-value>
</context-param>

It is then trivial to load the property at runtime if you have access to the servlet.

Properties properties = new Properties();
GenericServlet theServlet = ...;
String propertyFileName = theServlet.getInitParameter("propfile");
properties.load(getClass().getClassLoader().getResourceAsStream(propertyFileName));
Object myProperty = properties.get("myProperty");
like image 137
Ken Blair Avatar answered Oct 16 '22 03:10

Ken Blair


If using different environments it's very likely that you won't be switching from one to another at runtime, thus not needing to use a properties file.

If using maven, you can define different profiles for your environments and set the parameter you want to change in each profile.

In your pom.xml

<profile>
    <id>env1</id>
    <properties>
        <my.param>myParamValue<my.param/>
    </properties>
</profile>

<profile>
    <id>env2</id>
    <properties>
        <my.param>myParamValue2<my.param/>
    </properties>
</profile>

In your web.xml

<context-param>
    <param-name>myparam</param-name>
    <param-value>${my.param}</param-value>
</context-param>

And configure filtering in your deployment descriptor in maven war plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
    </configuration>
</plugin>
like image 20
Gabriel Molina Avatar answered Oct 16 '22 01:10

Gabriel Molina


AFAIK context-param and env-entry both hold static values. You will not get the runtime (dynamic) value from the property file. It will be like:

<context-param>     
  <param-name>myparam</param-name>     
  <param-value>myactualpropertyvalue</param-value>     
 </context-param>

Any change to the value needs a redeployment of the web app.

In your example, the value you retrieve would be the String classpath:mypropertyfile.properties['myproperty']

If you use Glassfish you can update it on the fly from commandline http://javahowto.blogspot.com/2010/04/glassfish-set-web-env-entry.html

If I understand your requirement is at build time (i.e different war for different env) and not during running time?

You could replace the values in web.xml as part of the ant/maven build process.

like image 2
JoseK Avatar answered Oct 16 '22 02:10

JoseK