Search code examples
tomcatpropertiessystem-propertiescontext.xml

How can I specify system properties in Tomcat configuration on startup?


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.


Solution

  • (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:

    <env-entry>
        <env-entry-name>SMTP_PASSWORD</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>abc123ftw</env-entry-value>
    </env-entry>