In addition to allowing us to configure web applications through annotations and through a web-fragment.xml
, Servlet 3.0 also allows us to configure our web applications programmatically at runtime.
The ServletContext
class has new methods to configure servlets, filters, and listeners programmatically. The following example illustrates how to configure a servlet programmatically at runtime, without resorting to the @WebServlet
annotation or to XML:
package net.ensode.javaee8book.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import javax.servlet.annotation.WebListener; @WebListener() public class ServletContextListenerImpl implements ServletContextListener { @Override public void contextInitialized( ServletContextEvent servletContextEvent) ...