views:

182

answers:

2

In production we run tomcat behind apache, in development bare tomcat. How can I know inside the Servlet class if it is running behind apache or not?

+2  A: 

If this is to distinguish between development and production environment, then you will probably be better off by having this completely under your control instead of ad-hoc guessing (which will eventually break).

This could be "Is feature X set in JNDI?" or "Is property foo.bar set in c:/ourproject.properties". You should not rely on artifacts like "is class X loaded from a file or found inside a jar" (since that will break if you change application servers) or "Is http header line X present" since that is out of your control plus it may break if somebody else is using Apache as a frontend accellerator.

So, explicit configuration - it can be done quite easily :)

Thorbjørn Ravn Andersen
+2  A: 

This depends on how your Apache is talking to Tomcat.

If it's connected via HTTP (mod_proxy), you can check request.getRemoteAddr(). It will be the IP of the Apache, probably your internal IP. You can also check "Via" header to see if Apache is there.

If it's connected via AJP (mod_jk/mod_proxy_ajp), you can check request.getLocalPort() to see if it's the port of your JK connector.

ZZ Coder