9

I would like to know if there is possibility to set an attribute in web.xml by using property file. For example the web.xml:

<context-param>
  <param-name>Map.MyJNDI</param-name>
  <param-value>java:comp/env/jdbc/${my.computer}</param-value>
</context-param>

and application.properties would be:

# My computer's name
my.computer=eniac
peterh
  • 11,875
  • 18
  • 85
  • 108
Tomas Babic
  • 269
  • 5
  • 14
  • Check these: - http://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml - http://stackoverflow.com/questions/12099008/how-to-include-values-from-properties-file-into-web-xml – Chepech Nov 01 '13 at 17:34
  • Possible duplicate of [Properties file as init-param in web.xml](https://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml) – peterh May 29 '18 at 15:19

2 Answers2

2

you can not set value from Properties file but you can set the property file and read its at runtime.

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

then read the property file at runtime.

MyServlet myServlet = new MyServlet();

Properties  properties = new Properties();
//  get the properties file name 
String propfile = myServlet.getInitParameter("propfile");

// load the file 
properties.load(getClass().getClassLoader().getResourceAsStream(propfile));

// get the desire properties 
Object propName = properties.get("my.computer");
// out.println(propName.toString());

hope this help other too.

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
0

You can't have values substituted in web.xml like that.

One option I can suggest if possible just have a template web.xml with place holder for values and during build for each environment have a step in build process that will substitute required values from required properties file of that environment.

Gladwin Burboz
  • 3,519
  • 16
  • 15