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?
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.
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.
The session listener will be triggered when the session is created – sessionCreated: HttpSession session = request.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With