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

I understand that I can specify Tomcat system properties by passing arguments with the -D option, for example, " -Dmy.prop = value ".

I am wondering if there is a cleaner way to do this by specifying the property values ​​in the context.xml file or another tomcat configuration file. I would like to do this because, firstly, it’s easier to track my properties, and secondly, I run several contexts, and I don’t know how to specify context-specific properties using the -D parameter.

I am using Tomcat version 5.5.

+69
properties tomcat system-properties
Dec 16 '08 at 20:59
source share
7 answers

(Update: if I could delete this answer, I would, although since it was accepted, I cannot. I am updating the description to provide better guidance and to prevent people from using the bad practices outlined in the original answer).

You can specify these parameters through the context or environment parameters, for example, in context.xml. See the “Context Settings” and “Environment Entries” sections on this page:

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html

As @netjeff points out, these values ​​will be available through the Context.lookup (String) method, and not as system parameters.

Another way to specify these values ​​is to define the variables inside the web.xml file of the web application that you are deploying (see below). As @Roberto Lo Giacco points out, this is usually considered bad practice because the deployed artifact should not be environment specific. However, the following is a snippet of the configuration 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> 
+12
Dec 18 '08 at 3:22
source share

The original cliff.meyers answer, which suggested using <env-entry> , would not help when using only System.getProperty ()

According to the docs of Tomcat 6.0 <env-entry> for JNDI . So this will not affect System.getProperty() .

When using the <env-entry> from the cliff.meyers example, the following code

 System.getProperty("SMTP_PASSWORD"); 

returns null, not the value "abc123ftw".

According to Tomcat 6 docs, to use <env-entry> , you need 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"); 

Warning. In fact, I have not tried the above example. But I had tried <env-entry> with System.getProperty (), and it definitely doesn't work.

+27
Jan 26 '09 at 23:01
source share

As a rule, you should not rely on system properties to configure webapp - they can be used to configure a container (for example, Tomcat), but not for an application running inside tomcat.

cliff.meyers has already mentioned how best to use your web application. This is the standard way, which is also suitable for your setup question through context.xml or server.xml.

However, if you really need system properties or other jvm parameters (for example, maximum memory settings) in tomcat, you must create a file called "bin / setenv.sh" or "bin / setenv.bat". These files do not exist in the downloadable standard archive, but if they are present, the contents are executed at startup (if you run tomcat through startup.sh/startup.bat). This is a great way to separate your own settings from tomcat default settings and make updating easier. No need to configure startup.sh or catalina.sh.

(If you are running tomcat as a windows service, you usually use tomcat5w.exe, tomcat6w.exe, etc. to configure registry settings for this service.)

EDIT: Also, another possibility is to upgrade to JNDI Resources .

+12
Dec 28 '08 at 14:49
source share

It is also possible for ServletContextListener to set the System properties:

 import java.util.Enumeration; import javax.servlet.*; public class SystemPropertiesHelper implements javax.servlet.ServletContextListener { private ServletContext context = null; public void contextInitialized(ServletContextEvent event) { context = event.getServletContext(); Enumeration<String> params = context.getInitParameterNames(); while (params.hasMoreElements()) { String param = (String) params.nextElement(); String value = context.getInitParameter(param); if (param.startsWith("customPrefix.")) { System.setProperty(param, value); } } } public void contextDestroyed(ServletContextEvent event) { } } 

And then put this in your web.xml (should be possible for context.xml too)

 <context-param> <param-name>customPrefix.property</param-name> <param-value>value</param-value> <param-type>java.lang.String</param-type> </context-param> <listener> <listener-class>servletUtils.SystemPropertiesHelper</listener-class> </listener> 

It worked for me.

+12
Feb 25 2018-10-25T00
source share

An alternative to setting the system property in tomcat configuration is to use the CATALINA_OPTS environment variable

+7
Sep 25 '12 at 23:27
source share

This issue is covered in the Apache wiki page.

Question: "Can I set Java system properties differently for each webapp?"

Answer: No. If you can edit Tomcat startup scripts (or better create a setenv.sh file), you can add the -D options in Java. But in Java there is no way to have different system property values ​​for different classes in the same JVM. There are other methods available, for example, using ServletContext.getContextPath () to get the context name of your web application and find the appropriate resources or define elements in the WEB-INF / web.xml file of your web application, and then set the values ​​for them in the Tomcat context file (META-INF / context.xml). See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html .

http://wiki.apache.org/tomcat/HowTo#Can_I_set_Java_system_properties_differently_for_each_webapp.3F

+5
Jul 20 '15 at 16:49
source share

You can add the necessary properties to the catalina.properties file in the <tomcat installation directory>/conf .

Link: https://tomcat.apache.org/tomcat-8.0-doc/config/index.html

All system properties are available, including those set using the -D syntax, automatically created by the JVM and those configured in the $ CATALINA_BASE / conf / catalina.properties file.

0
Nov 09 '17 at 21:13
source share



All Articles