Configuring web applications programmatically
In addition to allowing us to configure web applications through annotations and 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 com.ensode.jakartaeebook.servlet; //imports omitted @WebListener() public class ServletContextListenerImpl implements ServletContextListener { @Override public void contextInitialized( ServletContextEvent servletContextEvent) { ServletContext servletContext = servletContextEvent. getServletContext(); try { ProgrammaticallyConfiguredServlet...