Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using Ubuntu's default installation of Tomcat 6. I'm deploying a ROOT.war, and trying to set an environment variable specific to it, i.e. accessible from System.getenv() in the Servlet.init(config).

According to the docs (http://tomcat.apache.org/tomcat-6.0-doc/config/context.html), I can specify this in a Context element in conf/Catalina/localhost/ROOT.xml. I've created that with these contents:

<Context>
  <Environment name="FOO" value="bar" type="java.lang.String" override="false"/>
</Context>

And I've deployed the webapp as usual, i.e. to webapps/ROOT.war.

Server.getenv("FOO") in the Servlet.init(config) still returns null. What am I missing?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

From the Tomcat users mailing list:

It's not entirely clear from the Tomcat documentation, but you aren't creating environment variables that can be retrieved using System.getenv. Instead, you are placing entries into the JNDI context.

You'll want to retrieve them like this:

// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env");

String FOO = (String)envCtx.lookup("FOO");

// now FOO should have the value "bar" from your

The documentation for JNDI resources can be found here: http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html

Hope that helps, - -chris

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.