Jonathan Mast wrote:
> Andre, its one single, physical app/docBase, mapped to multiple contexts
> (which happen to located on different virtual hosts).  This is a
> requirement.
> 
> The /META-INF/context.xml approach is ruled out by this requirement.
> 
> My goal is to have a layer of code takes a the current host (eg.
> apples.mysite.com) looks it up in a database where it is linked to
> host-specific content.  After this stage, the showFruit.jsp will display an
> Apple and so on.
> 
>> - have your webapp (actually I guess, the first servlet) in it's init()
> code, get the hostname from getServerName() and
>> perform whatever setup it needs to. Then save this in an attribute of the
> ServletContext
> 
> But where do I find this elusive init() method?

Use a context listener.

Other tips you might find useful:
- make sure each virtual host has its own work directory
- rename your app ROOT.war rather than fruit.war (or ROOT rather than
fruit if deployed as a dir) to prevent double deployment
- turn off unpackWARs for all the hosts if deploying as a WAR

Mark

> 
> On Wed, Jun 24, 2009 at 5:01 PM, André Warnier <a...@ice-sa.com> wrote:
> 
>> Hi.
>>
>> I am one of the least Tomcat and Java qualified people that regularly lurk
>> on this forum, so don't take my word for any of what follows.
>> Let's say that I am just trying to apply what I think I have learned here.
>> And I am eager for contradiction, because it is said that this is how one
>> learns.
>>
>> Jonathan Mast wrote:
>>
>>> I have a webapp that I would like to behave in a context (actually
>>> host)-specific manner.  Where is the best place to initialize the
>>> context/host specific functionality?
>>>
>>> Let me demonstrate what I'm talking about.  Lets say I have a webapp Fruit
>>> located in folder webapps/fruit.
>>> I want to define:
>>> apples.mysite.com
>>> bananas.mysite.com
>>> coconuts.mysite.com
>>> etc  ...
>>> all of which point to webapps/fruit  (these are hosts with a "/" context
>>> pointing to "webapps/fruit" as the docBase, to be more precise).
>>>
>> appBase ?
>>
>>>
>> Do you mean all Hosts point to the *same physical* webapps/fruit, or does
>> each Host have its own copy in a separate directory ?
>>
>>  When someone visits apples.mysite.com they see an apple, when they visit
>>> bananas.mysite.com they see a banana, and so on.
>>>
>>> Where in the fruit app is the best place for instance of Fruit to
>>> introspect
>>> itself (basically look for what host name it is defined under) and prepare
>>> accordingly?
>>>
>>> I've looked into using Context Parameters in the server.xml declarations
>>>
>> That would probably better be in a /META-INF/context.xml, no ?
>> (at least if these are distinct webapp/fruit)
>>
>> or see here for more complete info :
>> http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
>>
>> but
>>
>>> I would like to avoid this if possible b/c this functionally is more
>>> elegantly determined through introspection (the web-app saying "what host
>>> do
>>> i belong to?").
>>>
>>> Of course I could always call request.getLocalName(),
>>>
>> I think you want getServerName(), or you'd always get the same DNS name/IP,
>> no matter wich virtual Host is called..
>>
>> but that would be
>>
>>> inefficient as it would have to be invoked on every request.
>>>
>>> I guess what I'm looking for someplace in the context initialization
>>> process
>>> where i could hook into and do my stuff and have it apply to the entire
>>> context throughout it's lifecycle.  Can't seem to find it digging around
>>> the
>>> javax.servlet.* javadocs.
>>>
>>>
>> I reason as follows :
>> - a webapp is run by a thread
>> - a thread is started by a Connector
>> - I don't think that a thread is Host-specific, in the sense that it can
>> run one webapp for one Host, and the next instant run another webapp for
>> another Host.
>>
>> What I'm saying is that I am not sure that above the Request level, you
>> will find anything that is "Host-persistent" to keep your stuff in and
>> retrieve it (I mean for webapps shared by several Hosts, which is probably a
>> bad idea anyway).
>>
>> To this eager student thus, the correct way to do what I understand you
>> want to do, seems to be :
>> - have each Host have its own appBase (webapp dir), with in each a copy of
>> your (identical) webapp code "Fruit".
>> - have your webapp (actually I guess, the first servlet) in it's init()
>> code, get the hostname from getServerName() and perform whatever setup it
>> needs to. Then save this in an attribute of the ServletContext
>> - which should then be available at each subsequent execution of any
>> servlet composing the webapp
>>
>> Inspired by the Servlet Spec 2.5, section 2.3 Servlet lifecyle, 2.3.2
>> Initialization.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>
>>
> 



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

Reply via email to