Hi,

Recently I am working on implementing a feature that can automatically
register a ServletContextListerner instance, say A, to servletContext
programmatically during startup.

I use ServletContainerInitializer and call the
servletContext.addListener() method. This is fine for most of the
cases. But if user has specified A in web.xml, it turns that A will be
registered twice, which will lead to undesired behavior.

My expected behavior will be registering A only once regardless of
user specified A in web.xml or not.

I can achieve this by specifying A in web-fragment.xml, since it will
get merged into web.xml whenever A is defined in web.xml or not, which
means duplicates can be removed.

I am wondering why we can not achieve this using programatic API.

I've checked servlet 4.0/3.1 spec, in 4.4.3.1 "void addListener(String
className)", it says:

If the class with the given name implements a listener interface whose
invocation order corresponds to the declaration order, that is, if it
implements javax.servlet.ServletRequestListener,
javax.servlet.ServletContextListener or
javax.servlet.http.HttpSessionListener, then the new listener will be
added to the end of the ordered list of listeners of that interface.

It looks tomcat's behavior is spec-compliant.

My idea is if I can get all the existing listeners from
servletContext, then I can decide whether to add A or not.

But I can't find that kind of API from servletContext.

I also noticed that there are getServletRegistrations() and
getFilterRegistrations() in servletContext, why can't we have API like
getListeners()?


-- 
Best Regards!
Huxing

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to