views:

1740

answers:

3

Hi,

I got a bunch of servlet context listeners in my Java webapp, each of them gathering some information about the environment.

Some of them depend on information which is gathered by another listener. But I can't determine the order in which the listeners are registered and called, so I have to duplicate code.

I understand that the listeners are registered in the order their order in web.xml but this sounds a bit vague to me, too vague to rely on it.

Do you have a hint how I can solve my problem?

A: 

It would seem you can create a ListenerManager which you place as your one and only listener, and have it contain the other Listeners. When the event comes in, simply call each of the other Listeners in the order you require (probably the order you created them in the ListenerManager's constructor).

Unfortunately, this means a code change when you add Listeners, but you can avoid this through clever use of properties files and create-from-class-name code.

Bill James
+2  A: 

Why is that vague? The ordering in web.xml is very specifically the order in which they are called, it's very clearly stated in the JEE spec. It's completely OK to rely on it.

skaffman
+3  A: 

All servlet containers an J2EE containers implement this part of the spec strictly. You can rely on the fact that the listeners are called in the order you specified in web.xml.

You can have a Application LEVEL Data structure(HashMap) that will be updated by each Filter/Listener as it encounters the data from the requests. This will let each Listener update only what is essential. You can put the common code in a base Listener so that there is no code duplication.

anjanb