tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Setting Environment Variable for Tomcat 6 Servlet
Date Fri, 21 Jan 2011 21:56:36 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Alyssa,

On 1/21/2011 4:29 PM, Alyssa Kwan wrote:
> 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?

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk06ARQACgkQ9CaO5/Lv0PDe9wCgmnx/XtBUNRa6xcGNpt5dGko0
9wgAnAuCWaMK1ZGwZgGwyJLYjh0R7kjW
=nBdC
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message