Programmatic configuration of listeners, filters, and servlets
Learn about the configuration methods, addListener, addFilter, and addServlet, which were introduced in the Servlet 3.0 specification. You can call these methods, which are part of the ServletContext interface, from a ServletContainerInitializer or a ServletContextListener.
Configuration methods, addListener, addFilter, and addServlet, are described in detail. ServletContextListener cannot programmatically configure servlets, filters, or listeners if the ServletContextListener is not declared in the web.xml file or web-fragment.xml file, or was not annotated with @WebListener. Therefore, any call on the ServletContext to perform such programmatic configuration results in an UnsupportedOperationException.
addListener
- Use the following method to add a listener to this servlet
context:
void addListener(java.lang.String className)
- Use the following method to add a listener to this servlet
context:
<T extends java.util.EventListener> void addListener(T t)
- Use the following method to add a listener to this servlet
context:
void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
- ServletContextAttributeListener
- ServletRequestListener
- ServletRequestAttributeListener
- HttpSessionListener
- HttpSessionAttributeListener
- javax.servlet.http.HttpSessionIdListener
- javax.servlet.ServletContextListener Note: A javax.servlet.ServletContextListener can only be added from the onStartUp method of a javax.servlet.ServletContainerInitializer.
addFilter
- Use the following method to add a filter to this servlet
context:
addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
- Use the following method to add a filter to this servlet
context:
addFilter(java.lang.String filterName, Filter filter)
- Use the following method to add a filter to this servlet
context:
addFilter(java.lang.String filterName, java.lang.String className)
addServlet
The addServlet methods dynamically adds servlets to a servletContext. These methods add the servlet with the given parameters to the web application context. The registered servlet might be further configured using the returned ServletRegistration object.
- Use the following method to add a servlet to this servlet
context:
addServlet(java.lang.String servletName, java.lang.Class<? extends Servlet> servletClass)
- Use the following method to add a servlet to this servlet
context:
addServlet(java.lang.String servletName, Servlet servlet)
- Use the following method to add a servlet to this servlet
context:
addServlet(java.lang.String servletName, java.lang.String className)
ServletContainerInitializer
When you configure a JAR file for a shared library and a ServletContainerInitializer is discovered within the JAR, the ServletContainerInitializer is invoked for every application that the shared library associates with.
Deprecated classes in Servlet 3.0
- public void addDynamicServlet(String servletName, String servletClass, String mappingURI, Properties initParameters) throws ServletException, java.lang.SecurityException;
- public void removeDynamicServlet(String servletName) throws java.lang.SecurityException