I understand that I can specify system properties to Tomcat by passing arguments with the -D parameter, for example "-Dmy.prop=value".
I am wondering if there is a cleaner way of doing this by specifying the property values in the context.xml file or some other tomcat configuration file. I would like to do this because, first, it is easier to keep track of my properties, and second, I have multiple contexts running and I don't know how I would specify context-specific properties through the -D parameter.
I am using Tomcat version 5.5.
Generally you shouldn't rely on system properties to configure a webapp - they may be used to configure the container (e.g. Tomcat) but not an application running inside tomcat.
cliff.meyers has already mentioned the way you should rather use for your webapplication. That's the standard way, that also fits your question of being configurable through context.xml or server.xml means.
That said, should you really need system properties or other jvm options (like max memory settings) in tomcat, you should create a file named "bin/setenv.sh" or "bin/setenv.bat". These files do not exist in the standard archive that you download, but if they are present, the content is executed during startup (if you start tomcat via startup.sh/startup.bat). This is a nice way to separate your own settings from the standard tomcat settings and makes updates so much easier. No need to tweak startup.sh or catalina.sh.
(If you execute tomcat as windows servive, you usually use tomcat5w.exe, tomcat6w.exe etc. to configure the registry settings for the service.)
EDIT: Also, another possibility is to go for JNDI Resources.
It's also possible letting a ServletContextListener set the System properties:
And then put this into your web.xml (should be possible for context.xml too)
It worked for me.
You could add necessary properties to
catalina.properties
file in<tomcat installation directory>/conf
directory.Reference: https://tomcat.apache.org/tomcat-8.0-doc/config/index.html
If you want to define an environment variable in your context base on documentation you shod define them as below
Also use them as below:
You should get
10
as output.(Update: If I could delete this answer I would, although since it's accepted, I can't. I'm updating the description to provide better guidance and discourage folks from using the poor practice I outlined in the original answer).
You can specify these parameters via context or environment parameters, such as in context.xml. See the sections titled "Context Parameters" and "Environment Entries" on this page:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
As @netjeff points out, these values will be available via the Context.lookup(String) method and not as System parameters.
Another way to do specify these values is to define variables inside of the web.xml file of the web application you're deploying (see below). As @Roberto Lo Giacco points out, this is generally considered a poor practice since a deployed artifact should not be environment specific. However, below is the configuration snippet if you really want to do this:
cliff.meyers's original answer that suggested using
<env-entry>
will not help when using only System.getProperty()According to the Tomcat 6.0 docs
<env-entry>
is for JNDI. So that means it won't have any effect onSystem.getProperty()
.With the
<env-entry>
from cliff.meyers's example, the following codewill return null, not the value "abc123ftw".
According to the Tomcat 6 docs, to use
<env-entry>
you'd have to write code like this to use<env-entry>
:Caveat: I have not actually tried the example above. But I have tried
<env-entry>
with System.getProperty(), and that definitely does not work.