Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing Environment Variables in web.xml

I'm pre-packaging a JSP web-app that relies on some file path settings found within web.xml. These settings are unknown at packaging time, because they reference a path the customer will set when deploying the entire application (of which the web-app is a management interface).

It seems that the easiest way to avoid tokens and file modifications in my installer script, is to ask the user for an install location, set this location as an environment variable (e.g JAVA_HOME), and have web.xml always reference that variable.

Is there a way to reference an environment variable value from within web.xml? Google searches lead to the J2EE method of SETTING environment variables from ejb xml files. This is not what I'm looking for.

like image 518
Udi Bar-On Avatar asked Sep 10 '09 10:09

Udi Bar-On


People also ask

Can you use environment variables in xml?

Answer. Yes, you can set and use environment variables and use system environment variables, but you must enclose environment variable references in brackets "{}" in deployment. xml.

What is ENV entry in Web xml?

Env entries are similar to providing init-params for a servlet or filter, or context-params for your application, except they use JNDI. A single <env-entry> tag is used for each property. The property name and value are set using <env-entry-name> and <env-entry-value>.

How are environment variables used in pom xml?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.

Can a browser access environment variables?

A web page doesn't have access to OS variables, so you can't normally use them. The solution is pretty simple: you just need to generate a file that contains them.


1 Answers

You can use Ant-style variable substitution in any of the tomcat xml config files, such as:

<servlet-mapping>     <servlet-name>mvc-dispatcher</servlet-name>     <url-pattern>${foo}</url-pattern> </servlet-mapping> 

Where foo is a Java System Property (sysprop).

You can't use OS Environment Variables (envvars) directly, I think...

To use envvars, you can put

set "CATALINA_OPTS=-DsomeJavaSysProp=%SOME_OS_ENVVAR%" 

in bin/setenv.bat (or similarly in bin/setenv.sh for *nix). You may need to create that file. Tomcat will run this file when it starts.

As CATALINA_OPTS is an envvar (as opposed to a command line option), it should not be visible by other users on the system (save ancient Unixes), though I haven't tested this.

http://tomcat.apache.org/tomcat-7.0-doc/config/

If you are using Spring, you can create a <context:property-placeholder/> bean and then directly use envvars or sysprops in Spring XML config files (though not web.xml).

like image 134
Neil McGuigan Avatar answered Sep 25 '22 17:09

Neil McGuigan