Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the order of listeners in web.xml

Tags:

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?

like image 819
Olvagor Avatar asked Oct 07 '08 13:10

Olvagor


People also ask

Does order matter in web xml?

For web. xml, yes. The servlet XSD (or DTD if you're using an older version) requires that the elements be listed in a specific order.

What is a listener in web xml?

Servlet Listener is used for listening to events in a web container, such as when you create a session or place an attribute in a session or if you passivate and activate in another container, to subscribe to these events you can configure listener in web. xml, for example, HttpSessionListener.

Which of the following listeners are invoked when a session is created?

The session listener will be triggered when the session is created – sessionCreated: HttpSession session = request.

What is event listener in Servlet?

Event Listener InterfacesInterface for receiving notifications about requests entering and exiting a web application's scope. javax.servlet.ServletRequestAttributeListener. Interface for receiving ServletRequest attribute changes notification events.


2 Answers

All servlet containers and Java EE 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.

like image 151
anjanb Avatar answered Oct 22 '22 05:10

anjanb


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 Java EE spec. It's completely OK to rely on it.

like image 23
skaffman Avatar answered Oct 22 '22 03:10

skaffman