Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a parameter for a Java Web application

I have a web app in Java, which uses some external program (invokes a command line tool).

I want to make the path of the command line program configurable, so that I can change it without re-building my application.

Questions:

1) Which exactly parameter should I use (out of those available in web.xml), if it is set only once (at deployment) and thereafter never changes?

2) How can I access this parameter inside my Java code?

Thanks in advance

Dmitri

like image 249
Dmitrii Pisarenko Avatar asked May 31 '11 12:05

Dmitrii Pisarenko


1 Answers

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
  <context-param>
    <param-name>command</param-name>
    <param-value>SOME_COMMAND</param-value>
  </context-param>
.
.
.
.
</web-app>

Java code

String commandToExecute =  getServletContext().getInitParameter("command");

Alternatively

You can also put this thing in property/xml file in the classpath read it and put it to servlet context when context initializes.

like image 102
jmj Avatar answered Oct 22 '22 08:10

jmj