You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Kevin McAllister <ke...@centraview.com> on 2004/06/09 18:26:46 UTC

Getting Host information from within webapp

Hello,

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 think I would be able to retrieve this 
information utilizing the JMX stuff, however, it is not clear to me how 
I would be able to tell which host name retrieved would be the one 
serving my context.

Particularly I would like to be able to do this within a class 
implementing the org.apache.struts.action.Plugin interface in the init() 
method.  So I can't do it as easily as parsing the request, plus I would 
want the name attribute and not an Alias which I could possibly get from 
the request.

I have not seen any methods on the ServletContext(), or the 
ServletConfig object.C an anyone provide information on how I would be 
able to do this, or point me further in the right direction?

Thanks,
Kevin

-- 
Kevin McAllister
CentraView, LLC
http://www.centraview.com/

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


Re: Getting Host information from within webapp

Posted by Kevin McAllister <ke...@centraview.com>.
Tim Funk wrote:
> Would request.getServerName() work?
> 

Ordinarily that would be sufficient, but in this case, I want to know 
the actually host name attribute, as getServerName parses information 
from the request it could possibly contain the name of an Alias or an IP 
address, or neither in the case that the host in question was the 
defaultHost for the Engine.

But Thanks.

- Kevin


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


Re: Getting Host information from within webapp

Posted by Tim Funk <fu...@joedog.org>.
Would request.getServerName() work?

-Tim

Kevin McAllister wrote:

> 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 think I would be able to retrieve this 
>> information utilizing the JMX stuff
> 
> 
> Upon doing some further research I found I would be able to get a host 
> name given a context as long as my webapp was privileged (so the 
> org.apache.catalina.mbean.ServerLifecycleListener would place a 
> reference to the MBeanServer in the ServletContext attributes) However, 
> it seems the only information I would have to Query the MBeanServer is 
> the context path, And if I have more than one context path which is the 
> same (on different Hosts obviously) then I would not be able to uniquely 
> Identify the Host to which I am bound.  And since I will most certainly 
> have many Hosts each with the same context path for my webapp, this 
> method will not work for my purposes.
> 
> Hopefully the information is available somewhere else, or otherwise I 
> may be able to obtain it by inserting another ServerLifeCycleListener 
> that can gather the information from the context adding event and put it 
> on the ServletContext attributes.
> 
> - Kevin
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> 
> 

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


Re: Getting Host information from within webapp

Posted by Kevin McAllister <ke...@centraview.com>.
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 think I would be able to retrieve this 
> information utilizing the JMX stuff

Upon doing some further research I found I would be able to get a host 
name given a context as long as my webapp was privileged (so the 
org.apache.catalina.mbean.ServerLifecycleListener would place a 
reference to the MBeanServer in the ServletContext attributes) However, 
it seems the only information I would have to Query the MBeanServer is 
the context path, And if I have more than one context path which is the 
same (on different Hosts obviously) then I would not be able to uniquely 
Identify the Host to which I am bound.  And since I will most certainly 
have many Hosts each with the same context path for my webapp, this 
method will not work for my purposes.

Hopefully the information is available somewhere else, or otherwise I 
may be able to obtain it by inserting another ServerLifeCycleListener 
that can gather the information from the context adding event and put it 
on the ServletContext attributes.

- Kevin

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


Re: Getting Host information from within webapp

Posted by Kevin McAllister <ke...@centraview.com>.
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