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

The addGlobalListener method is deprecated in WebSphere® Application Server Version 9.0. It is replaced with the addListener method.
  • 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)
The listener class must implement one or more of the following interfaces:
  • 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

The addMappingFilter method is deprecated in WebSphere Application Server Version 9.0. It is replaced with the addFilter method. This method adds the filter with the given name, description, and class name to the Web application context. The registered filter might be further configured using the returned FilterRegistration object.
  • 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

The following classes are deprecated from com.ibm.websphere.servlet.context.IBMServletContext:
  • 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
There is no replacement for the removeDynamicServlet method because removing a servlet can lead to timing issues if a request was servicing that servlet at the same time. The addServlet and createServlet methods replace the addDynamicServlet method.