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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to