Set Environment Variable in Tomcat tomcat logo

Set Environment Variable in Tomcat

In this post, we will learn to set environment variables in the tomcat configuration file so that when the application loads then it will pick a value automatically from there.


Let’s say, we have a Spring Boot application packaged as a war file and we want to deploy it to an external Tomcat server.

In this case, whatever the profile we’ve defined in our file it will automatically pick from the tomcat configuration file.

We might have different configurations for different environments. But, once we package our code to a war file then it is unlikely to repackage again and again when we want to deploy to other environments like test, staging, and production.

Hence, to use the same packaged war file in multiple environments we have to set up an environment variable in tomcat’s context.xml file.


The context.xml file can be found inside a conf directory of our external tomcat.

Open the context.xml file and define an environment like the following:

<!-- Write following line at the end of this file before closing the Context tag.                                   -->
    <Environment name="" type="java.lang.String" value="staging" />


Now, we can save this file.

After saving this, it is equivalent to the following configuration of file in our spring boot application:


In this post, we learned to set the environment name and value in our external tomcat server. By setting this, we can load the value of the environment name from the context.xml file.

Notify of
Inline Feedbacks
View all comments