tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Chaney <>
Subject Re: The best place for implementing context specific behavior?
Date Thu, 25 Jun 2009 20:54:08 GMT

André Warnier wrote:
> Pid wrote:
>> The Filter checks the domain name is suitable, /looks up a config object/
>> and drops it into the request attributes for use on the page.
> Ha ! I believe that this is exactly what Jonathan is (or was originally) 
> looking for.
> Where is this "config object", and when and how is it being initialised ?
One place to keep filter configuration information is as an attribute of 
the ServletContext. This can be obtained with the 
FilterConfig.getServletContext method. The FilterConfig object is a 
parameter of the doFilter method of the Filter. (See

This works within any one web app.

> I'm continuing for myself right now, just to learn how to do this 
> correctly.
> Suppose I do want to do something different in my (shared, common, 
> whatever) webapp, depending on the "Host:" header of the request.
> And suppose this different thing I want to do, is a bit heavy, so I 
> don't want to do it all at each request, I want to do some of that work 
> ahead of time, and re-use it for each request afterward.
> But the requests come in unpredictable order, all to my same webapp, but 
> one for "", and the following one for 
> "" etc.
> And when one comes in for "bananas", I want to retrieve what I had 
> earlier prepared for "bananas", and not what I had prepared for "lemons".
> (In other words, I don't want a fruit salad, I want to enjoy each fruit 
> separately).

You can write an object which handles the specific processing for each 
host and then create a Map as the attribute in the ServletContext using 
the host name as the key. At the start of each request in your Filter 
you could retrieve the object by using the host name as the key, do 
whatever host specific processing you need.

You can add a ContextListener in web.xml will will allow you to do any 
servlet startup/shutdown operations.

There are other ways, but the above should work.


Alan Chaney

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message