Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServletContextListener execution order

How to define order of ServletContextListener's execution due application initialization, if i have multiple ServletContextListener's and some of them declared in deployment descriptor and other with annotation (@WebListener)?

like image 629
Timur Samkharadze Avatar asked Nov 01 '13 15:11

Timur Samkharadze


People also ask

What is the use of Servletcontextlistener?

Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.

How does servlet listener work?

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.

What is context listener in Java?

A context listener receives notifications when the web application (ie: the context) is starting up or shutting down.


2 Answers

If you want to execute listeners in a particular order, you should use deployment descriptor to define them.

Below statements are copied from Servlet Specification:

8.2.3:

If the order in which the listeners, servlets, filters are invoked is important to an application then a deployment descriptor must be used. When using annotations to define the listeners, servlets and filters, the order in which they are invoked is unspecified.

The ordering will be based on the order in which they are defined in the descriptor and on the absolute-ordering element in the web.xml or an ordering element in the web-fragment.xml.

Prior to this release of the specification (Java™ Servlet Specification, version 3), context listeners were invoked in random order. As of Servlet 3.0, the listeners are invoked in the order in which they are declared in the web.xml.

Implementations of javax.servlet.ServletContextListener are invoked at their contextInitialized method in the order in which they have been declared, and at their contextDestroyed method in reverse order.

If you have multiple ServletContextListeners and some of them are declared in deployment descriptor and others with annotation, then its the listeners defined in web.xml that will get the precedence. Below statement is copied from the same section (8.2.3) of servlet specification:

Configuration specified in the main web.xml or a web fragment takes precedence over the configuration specified via annotations.

like image 151
Debojit Saikia Avatar answered Sep 21 '22 15:09

Debojit Saikia


It seems you know the order of execution but you want to know what will it be if some listeners are declared in deployment descriptor and some using annotation so the preference is taken by deployment descriptor and then annotation.

Note the constructors of all listeners will be called first in order of deployment descriptor & then annotation and then the life cycle methods in same order.

like image 37
Abdullah Shaikh Avatar answered Sep 20 '22 15:09

Abdullah Shaikh