Servlet 3.0 programmatic configuration

The configuration methods, addListener, addFilter, and addServlet are introduced in the Servlet 3.0 specification.

The methods for Servlet 3.0 are part of the ServletContext interface. You can call these methods from either a ServletContainerInitializer or a ServletContextListener.

addListener

The addGlobalListener method is deprecated in WebSphere® Application Server Version 8.5. It is replaced with the addListener method.
  • Use the following method to add the listener with the given class name to this servlet context:
    void addListener(java.lang.String className)
  • Use the following method to add the given listener to this servlet context:
    <T extends java.util.EventListener> void addListener(T t)
  • Use the following method to add a listener of the given class type to this servlet context:
    void addListener(java.lang.Class<? extends java.util.EventListener> listenerClass)
The given listener class must implement one or more of the following interfaces:
  • ServletContextAttributeListener
  • ServletRequestListener
  • ServletRequestAttributeListener
  • HttpSessionListener
  • HttpSessionAttributeListener

addFilter

The addMappingFilter method is deprecated in WebSphere Application Server Version 8.5. 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 the filter with the given name and class type to this servlet context:
    addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
  • Use the following method to register the given filter instance with this servlet context under the given filterName:
    addFilter(java.lang.String filterName, Filter filter) 
  • Use the following method to add the filter with the given name and class name 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 will 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 the filter with the given name and class type to this servlet context:
    addFilter(java.lang.String filterName, java.lang.Class<? extends Filter> filterClass)
  • Use the following method to register the given filter instance with this servlet context under the given filterName:
    addFilter(java.lang.String filterName, Filter filter)
  • Use the following method to add the filter with the given name and class name to this servlet context:
    addFilter(java.lang.String filterName, 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.