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

La méthode addGlobalListener est dépréciée dans WebSphere Application Server Version 9.0. Elle est remplacée par la méthode 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)
The listener class must implement one or more of the following interfaces:
  • 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

La méthode addMappingFilter est dépréciée dans WebSphere Application Server Version 9.0. Elle est remplacée par la méthode addFilter. Cette méthode ajoute le filtre ayant le nom, la description et le nom de classe indiqués au contexte de l'application Web. Le filtre ainsi enregistré peut ensuite être configuré à l'aide de l'objet FilterRegistration retourné.
  • 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

Les classes suivantes sont dépréciées dans 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
Aucun remplacement n'est prévu pour la méthode removeDynamicServlet, car le fait de supprimer un servlet peut conduire à des problèmes de chronologie si une demande est servie par ce servlet au moment où il est supprimé. Les méthodes addServlet et createServlet remplacent quant à elles la méthode addDynamicServlet.

Icône indiquant le type de rubrique Rubrique de concept



Icône d'horodatage Dernière mise à jour: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=cweb_servlet30configmethods
Nom du fichier : cweb_servlet30configmethods.html