Skip to main content

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

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



Back to the top