1

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?

jennykwan
  • 141
  • 7

1 Answers1

3

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 <Environment>

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
xOneca
  • 41
  • 1
  • 10
jennykwan
  • 141
  • 7