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 Remarque : 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
La méthode addServlet ajoute dynamiquement un servlet au contexte de servlet (servletContext). These methods add the servlet with the given parameters to the web application context. Le servlet ainsi enregistré peut ensuite être configuré à l'aide de l'objet ServletRegistration retourné.
- 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
Lorsque vous configurez un fichier JAR en vue d'utiliser une bibliothèque partagée et qu'un ServletContainerInitializer est découvert dans ce fichier JAR, le ServletContainerInitializer est invoqué pour chaque application à laquelle la bibliothèque partagée est associée.
Classes dépréciées dans 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