Question

I know what ServletContextListener is and how to implement/register it in web.xml.

But the question is the following. At the start of web application single instance of listener per javaVM is created. Then its contextInitialized() method is called.

At the moment when application is being closed, contextDestroyed() method of listener is called. But is it the same instance of listener? Or that first instance could be destroyed and new one created for contextDestroyed() call?

Was it helpful?

Solution

Definitive proof would have to come from the Servlet spec. I'm not sure that it provides an explicit answer to your question, but it does provide a strong hint:

SRV.10.3.3 Listener Registration
The Web container creates an instance of each listener class and registers it for event notifications prior to the processing of the first request by the application.

Which implies that creating instances after processing the first request is prohibited.

OTHER TIPS

How about putting some code in both contextInitialized() and contextDestroyed().

Something like this:

System.out.println(this.toString())

Assuming that you don't override default toString method of Object you should see the hashCode of the object in Hexadecimal (@8104b1) and by doing that you can check if it is the same object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top