I have a web.xml file and i want to read multiple values from the properties file into web.xml. Is this possible? . And if yes,How?.Can I access this properties file in java class itself? I am specifically using ant to build my project up. Any help regarding this is appreciated.
Asked
Active
Viewed 1,242 times
2 Answers
3
You can do that with placeholders. For example (with Gradle):
gradle.properties:
myProp1=abc
myProp2=def
build.gradle:
war {
eachFile {
if (it.name == 'web.xml') {
it.filter {String line -> line.replaceAll('\\$\\{textToReplace1\\}', myProp1) }
it.filter {String line -> line.replaceAll('\\$\\{textToReplace2\\}', myProp2) }
}
}
}

Cengiz
- 5,375
- 6
- 52
- 77
0
properties.load(Connector.class.getResourceAsStream("path/to/properties/file"));
this.DRIVER_CLASS = properties.getProperty("attribute/mentioned/in/the/property/file");
this.DATABASE_URL = properties.getProperty("another/attribute/mentioned/in/the/property/file");
this.databaseName = properties.getProperty("other/attribute/mentioned/in/the/property/file");
this.username = properties.getProperty("other/attribute/mentioned/in/the/property");
And so on...
Properties file is in the same format as shown above by "Cengiz". Here this.variables are my private variables which can be anything. Also Properties file needs to be at the top level of hierarchy as of classes.
This Solved my Query. Thank you for the help.

Apurv Nerlekar
- 2,310
- 1
- 21
- 29
-
I figured this out by not accessing properties file in web.xml but in java files directly. Took the other way round. But the bottom line was to get the code working. So did this.:) – Apurv Nerlekar Oct 23 '12 at 06:26