DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Optimizing Database Connectivity: A Comparative Analysis of Tomcat JDBC vs. HikariCP
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC

Trending

  • PII Detection Before RAG Ingestion: Screening Documents in C#
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents
  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Tomcat and Environment Entries

Tomcat and Environment Entries

Learn more about how to add environmental entries in Tomcat.

By 
Mohammad Nadeem
·
Jul. 01, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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:


Apache Tomcat

Published at DZone with permission of Mohammad Nadeem. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Optimizing Database Connectivity: A Comparative Analysis of Tomcat JDBC vs. HikariCP
  • Deployment of Spring MVC App on a Local Tomcat Server
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook