tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: server.xml hostname from ServletContext
Date Mon, 01 Oct 2012 23:11:58 GMT
2012/10/1 Mark Eggers <its_toasted@yahoo.com>:
> On 10/1/2012 8:38 AM, Aggarwal, Ajay wrote:
>>
>> Is the configured hostname available in ServletContext? I see it in
>> debugger, but I don't see any method to access it from ServletContext
>> class. I am using virtual hosts and need this value inside my
>> ServletContextListener ::contextInitialized() call back.
>>
>> Thanks.
>>
>> -Ajay
>
>
> I've not found a convenient way to manage this. Getting the host name when
> you are using multiple Host elements in your server.xml appears to return
> the host you're running on (and not the Host element name or alias).
>
> The easiest way I've found to do this is as follows.
>
> 1. Create a context.xml.default
>
> In each CATALINA_BASE/conf/[engine]/[hostname] create an XML file called
> context.xml.default. [engine] is usually Catalina. This default context gets
> added to all web applications in that [hostname].
>
> 2. Add a JNDI environment resource to the context.xml.default
>
> In each context.xml.default file, add a resource something like the
> following:
>
> <Context>
>     <Environment name="hostname" value="your-hostname-goes-here"
>                  type="java.lang.String" override="false"/>
> </Context>

Interesting idea.

The following will also work and the value will be easier to obtain:
     <Parameter name="hostname" value="your-hostname-goes-here"
                  override="false"/>

>
> 3. In your servlet context listener, read the information
>
> Something like this - and then do with it what you want.
>
> // lots of imports omitted
>
> ServletContext sc = sce.getServletContext();
> try {
>     Context initCtx = new InitialContext();
>     Context envCtx = (Context) initCtx.lookup("java:comp/env");
>     String hostname = (String) envCtx.lookup("hostname");
>     if (hostname != null) {
>         sc.setAttribute("hostname", hostname); // or anything else
>     }
> } catch (NamingException ex) {
>     // do something nice about logging here
> }
>

One other possible way:
- Implement a LifecycleListener and attach it to
org.apache.catalina.core.StandardContext class. That is, add it as
<Listener> to context.xml file.
- The host name could be obtained as StandardContext.getParent().getName()
- It can be passed to the web application as a context attribute, via
StandardContext.getServletContext().setAttribute(name, value)


> See the following documentation for Tomcat 6 (which is where I've tried
> this).
>
> http://tomcat.us.apache.org/tomcat-6.0-doc/jndi-resources-howto.html
> http://tomcat.us.apache.org/tomcat-6.0-doc/config/context.html
>

The server name above should be "tomcat.apache.org". The us mirror is
one of two servers behind it.

> I suspect it's the same in Tomcat 7. Read the appropriate documentation to
> find out.
>

Best regards,
Konstantin Kolinko

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


Mime
View raw message