Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value in web.xml using property file?

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
like image 331
Tomas Babic Avatar asked Jul 14 '10 20:07

Tomas Babic


1 Answers

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.

like image 101
Nikson Kanti Paul Avatar answered Oct 20 '22 05:10

Nikson Kanti Paul