Ivano,
On 12/31/24 3:56 AM, Ivano Luberti wrote:
Chris
Il 27-Dec-24 19:18, Christopher Schultz ha scritto:
Ivano,
On 12/27/24 12:48 PM, Ivano Luberti wrote:
Chris, first of all thanks for your response.
Il 27-Dec-24 18:37, Christopher Schultz ha scritto:
Ivano,
On 12/27/24 11:48 AM, Ivano Luberti wrote:
> Tomcat version is 9.0.96
>
> I have two questions:
>
> 2) The docs (https://tomcat.apache.org/tomcat-9.0-doc/servletapi/
javax/ servlet/ServletContextListener.html) state that
>
>> All servlets and filters have been destroyed before any
ServletContextListeners are notified of context destruction.
>>
> Now in my code I have a servlet class (MyServletClass) that
implements ServletContextListener and contextDestroyed
>
> and another not servlet class (say MyClass) that implements those
as well.
You have a Servlet that implements ServletContextListener, and also
includes a contextDestroyed(ServletContextEvent) method? Okay.
> Using breakpoints I see that MyClass.contextDestroyed called
BEFORE MyServletClass.contextDestroyed
>
> and for what I understand it should be the other way around.
For what reason? If they are both implementing
ServletContextListener, they should both be called. But there is no
guarantee of the order in which they are called.
The fact that one of them is a Servlet is not relevant.
The doc says:
All servlets and filters have been destroyed before any
ServletContextListeners are notified of context destruction.
Correct. So your servlet's destroy() method should be called before
any ServletContextListener.contextDestroyed() method is called, and
for your Servlet/ServletContextListener, you should get one call
before the second, reliably.
If you use a debugger (or just logging statements), you should be able
to confirm that.
In other words the docs are saying that for each servlet destroy() is
called before contextDestroyed). Right ?
/Every/ servlet should have .destroy() called before /any/ filter gets a
.contextDestroyed() call.
Then I think it should be stated more precisely since it can be
interpreted as all the servlets are destroyed before any
contextDestroyed() method is called in any class.
I believe we are saying the same thing.
Please note that Apache Tomcat is not responsible for the language of
the Servlet Specification. If you believe there is confusing language in
the spec, please file a bug against the Jakarta EE project here:
https://github.com/jakartaee/servlet
From Tomcat's perspective, it's just a ServletContextListener like
any other. I would bet that you get two separate instances of that
class in memory, as well: one to serve as the Servlet and another to
serve as the ServletContextListener. Forcing them to be the same
object might be difficult to accomplish.
I can't follow you: if I have a class like
public class MyServlet extends HttpServlet implements
ServletContextListener {
there will be two objects in memory?
Most likely, yes. Try this:
public class MyServlet extends HttpServlet implements
ServletContextListener {
public void init() {
getServletConfig().getServletContext().log(getClass().getName() + "
instance " + hashCode() + " init()");
}
public void destroy() {
getServletConfig().getServletContext().log(getClass().getName() + "
instance " + hashCode() + " destroy()");
}
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().log(getClass().getName() + " instance
" + hashCode() + " contextInitialized()");
}
public void contextDestroyedServletContextEvent sce) {
sce.getServletContext().log(getClass().getName() + " instance
" + hashCode() + " contextDestroyed()");
}
}
You should end up with log messages that look like this:
MyServlet instance 3876ace contextInitialized()
MyServlet instance ab65de init()
MyServlet instance ab65de destroyed()
MyServlet instance 3876ace contextDestroyed()
If that's not the case, please post source for a web application that
reproduces the issue and we will look at it.
I can confirm you are right.
Good :D
-chris
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org