Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [servlet-dev] Restrictions on what programmatically added listeners etc can do


Arjan,

I can't think why getting virtual host or accessing registrations is restricted for programmatically added listeners... in fact I can't see that Jetty actually enforces this for the virtual host?

Perhaps the restriction on the listener was there to avoid calling listeners that added listeners that needed to be called, that could add more listeners etc. during contextInitialized?     While a bit painful, such code is not impossible.... but if we decide to support it, we should call out this complexity in the javadoc.

Do you want to prepare a PR to relax this restriction and we can discuss in more detail there?

cheers





On Wed, 6 Feb 2019 at 05:55, arjan tijms <arjan.tijms@xxxxxxxxx> wrote:
Hi,

For among others programmatically added listeners (eg. ServletContextListeners) several methods on ServletContext can't be called. For instance:

     * @throws UnsupportedOperationException if this ServletContext was
     * passed to the {@link ServletContextListener#contextInitialized} method
     * of a {@link ServletContextListener} that was neither declared in
     * <code>web.xml</code> or <code>web-fragment.xml</code>, nor annotated
     * with {@link javax.servlet.annotation.WebListener}
     *
     * @since Servlet 3.1
     */
    public String getVirtualServerName();


I wonder, what's the reason for this?

One can now obtain the virtual server name from a statically defined listener that is called before the dynamic filter and use it anyway.

Same holds for obtaining the ServletRegistrations. This is now forbidden as well, but a static listener can do this:

@WebListener public class MappingInit implements ServletContextListener { 

    @Override 
    public void  contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setAttribute("mappings", sce.getServletContext().getServletRegistrations()); 
    } 
}

And then the dynamic listener can pick these up, and use them to add e.g. mappings.

So what's the exact reason for the restriction?

Kind regards,
Arjan


_______________________________________________
servlet-dev mailing list
servlet-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://www.eclipse.org/mailman/listinfo/servlet-dev


--

Back to the top