Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
Markus Avatar asked Dec 16 '08 20:12

Markus


People also ask

Where is Tomcat system property set?

Select Run > Run Configurations. Make sure your server is selected. Select Environment Tab.

Which file can be used to define Tomcat specific configuration options?

XML. The server. xml file is Tomcat's main configuration file, and is responsible for specifying Tomcat's initial configuration on startup as well as defining the way and order in which Tomcat boots and builds.

How do I set Catalina properties?

You could add necessary properties to catalina. properties file in <tomcat installation directory>/conf directory. All system properties are available including those set using the -D syntax, those automatically made available by the JVM and those configured in the $CATALINA_BASE/conf/catalina. properties file.


2 Answers

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 on System.getProperty().

With the <env-entry> from cliff.meyers's example, the following code

System.getProperty("SMTP_PASSWORD"); 

will 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>:

// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env");  // Look up our data source String s = (String)envCtx.lookup("SMTP_PASSWORD"); 

Caveat: I have not actually tried the example above. But I have tried <env-entry> with System.getProperty(), and that definitely does not work.

like image 52
netjeff Avatar answered Oct 09 '22 08:10

netjeff


(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> 
like image 44
cliff.meyers Avatar answered Oct 09 '22 08:10

cliff.meyers