Tomcat and Environment Entries
Learn more about how to add environmental entries in Tomcat.
Adding Environment Entries
Normally, in web.xml, you can add the following environment entries:
<env-entry>
<env-entry-name>maxExemptions</env-entry-name>
<env-entry-value>10</env-entry-value>
<env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
However, Tomcat has a better way of doing it, thereby providing an opportunity to have different values in a different environment without modifying the deployment descriptor.
This is called context.xml, which can be placed under META-INF\ or under conf\Catalina\localhost\
Here is an example:
<Context>
...
<Environment name="maxExemptions" value="10"
type="java.lang.Integer" override="false"/>
...
</Context>
Reading Environment Entries
Environment Entries are relative to java:comp/env:
Reading one property
Context ctx = new InitialContext();
String url = (String) ctx.lookup("java:comp/env/url");
Reading all properties
for (Enumeration<Binding> e = ctx.listBindings("java:comp/env"); e.hasMoreElements();) {
Binding bind = e.nextElement();
System.out.println(bind.getName() + " : " + bind.getObject());
}
Example
Clone the sample project and execute the following command:
mvn clean package docker:build docker:run
Note: Make sure Docker is installed beforehand.
Here is the output:
Published at DZone with permission of Mohammad Nadeem. See the original article here.
Opinions expressed by DZone contributors are their own.