tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin McAllister <ke...@centraview.com>
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 = 
(Object)servlet.getServletContext().getAttribute("org.apache.catalina.resources");
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 
org.apache.struts.action.PlugIn.

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


Mime
View raw message