tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin McAllister <>
Subject Re: Getting Host information from within webapp
Date Thu, 10 Jun 2004 16:29:31 GMT
Kevin McAllister wrote:
> I am using tomcat 4.1.29, and would like to obtain the host name 
> attribute from within my servlet code for the associated context in 
> which I am running.

I have finally found a solution that seems to work:
Object dirContext = 
Class dirContextClass = dirContext.getClass();
String host;
try {
   Method hostMethod = dirContextClass.getMethod("getHostName",null);
   host = (String)hostMethod.invoke(dirContext,null);
} catch (Exception e) {
   System.out.println("[Exception] Cannot retrieve hostname: " + e);

It is not a general solution that will work with any container 
obviously, as it relies on the 
org.apache.naming.resources.ProxyDirContext Method getHostName().

BTW in this snippet servlet is org.apache.struts.action.ActionServlet I 
am calling this from the init() method defined in the Interface 

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message