I have a class Initializerthat implements an interface ServletContextListener. In my method, contextInitialized()I initialize some global classes that need to be destroyed, otherwise the servlet cannot be unloaded.
However, when the servlet method init()throws ServletException, the method is contextDestroyed()never called → resources are not released → the servlet is not unloaded by Tomcat (it remains at the “start”) although its method initnever completed).
My question is, how can I clear the resources in this case?
Bonus: why does the servlet even get into a working state? I understand from the documentation that it should not be executed if the method init()does not complete successfully.
Change . I think this is because every status bar displayed in Tomcat Manager represents a whole war, not a servlet. A war can contain several servlets, some of which can start, and others not. InitializerIt is called when the container starts, and its destruction is called only when the entire container is deleted. This leads to a related question - is there a similar built-in way to monitor the status of individual servlets?
(I know that I can write my own code to monitor the servlet, either through JMX or without it, but this is not included in the scope of this question).
source
share