8

I need to include some values from a file.properties into the WEB-INF/web.xml something like this:

<param-name>uploadDirectory</param-name>
<param-value>myFile.properties['keyForTheValue']</param-value>

I'm currently working with this:

  • JBoss
  • JEE5
andrewsi
  • 10,807
  • 132
  • 35
  • 51
lancha90
  • 5,064
  • 2
  • 17
  • 9
  • Possible duplicate of [Using properties in web.xml](http://stackoverflow.com/questions/2948992/using-properties-in-web-xml) – imgx64 Jan 03 '17 at 04:25

3 Answers3

14

You can add this class, that add all properties from your file to JVM. And add this class like context-listener to web.xml

public class InitVariables implements ServletContextListener
{

   @Override
   public void contextDestroyed(final ServletContextEvent event)
   {
   }

   @Override
   public void contextInitialized(final ServletContextEvent event)
   {
      final String props = "/file.properties";
      final Properties propsFromFile = new Properties();
      try
      {
         propsFromFile.load(getClass().getResourceAsStream(props));
      }
      catch (final IOException e)
      {
          // can't get resource
      }
      for (String prop : propsFromFile.stringPropertyNames())
      {
         if (System.getProperty(prop) == null)
         {
             System.setProperty(prop, propsFromFile.getProperty(prop));
         }
      }
   }
}  

in web.xml

   <listener>       
      <listener-class>
         com.company.InitVariables
      </listener-class>
   </listener>  

now you can get all properties in you project using

System.getProperty(...)

or in web.xml

<param-name>param-name</param-name>
<param-value>${param-name}</param-value>
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • I couldn't get Jetty to find the resource using relative path and getResourceAsStream but an absolute path and FileInputStream seem to work... +1 for getting me in the right direction! – jsh Aug 06 '13 at 18:38
  • There isn't a utility for this? – user447607 Mar 07 '14 at 14:25
  • In java we'll be able to get property value anywhere in application but in xml we are not able to use a variable in web.xml. – Arnav Joshi Feb 02 '17 at 08:39
3

A word of caution regarding the accepted solution above.

I was experimenting with this on jboss 5 today: the contextInitialized() method doesn't get invoked until after web.xml is loaded so the change to System properties doesn't take effect in time. Strangely this means that if you re-deploy the webapp (without restarting jboss) the property will survive from being set the last time it was deployed, so it may appear to work.

The solution that we're going to use instead is to pass the parameters to jboss via the java command line e.g. -Dparameter1=value1 -Dparameter2=value2.

dannyclark
  • 76
  • 2
  • 1
    A better solution(doesn't work with jboss 7 though) is to use properties.service.xml. You can map properties on the fly there. – gebuh Jan 04 '13 at 17:15
  • It's a good thought (and this is where our sysadmins prefer to keep system properties) but have you got this to work? I tried updating the properties.services.xml under `server/default/deploy` and `server/all/deploy` and in either case, it doesn't take effect in the web.xml – dannyclark Jan 08 '13 at 08:54
-1

Use replacetoken task of Ant. https://blogs.oracle.com/rajeshthekkadath/entry/automation_using_ant_replace_function

RajV
  • 6,860
  • 8
  • 44
  • 62