Package javax.servlet

Class GenericFilter

  • All Implemented Interfaces:
    java.io.Serializable, Filter, FilterConfig
    Direct Known Subclasses:
    HttpFilter


    public abstract class GenericFilter
    extends java.lang.Object
    implements Filter, FilterConfig, java.io.Serializable

    Defines a generic, protocol-independent filter. To write an HTTP filter for use on the Web, extend HttpFilter instead.

    GenericFilter implements the Filter and FilterConfig interfaces. GenericFilter may be directly extended by a filter, although it's more common to extend a protocol-specific subclass such as HttpFilter.

    GenericFilter makes writing filters easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the FilterConfig interface.

    To write a generic filter, you need only override the abstract doFilter method.

    Since:
    Servlet 4.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      GenericFilter​()
      Does nothing.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      FilterConfig getFilterConfig​()
      Returns this servlet's ServletConfig object.
      java.lang.String getFilterName​()
      Returns the name of this filter instance.
      java.lang.String getInitParameter​(java.lang.String name)
      Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
      java.util.Enumeration<java.lang.String> getInitParameterNames​()
      Returns the names of the filter's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the filter has no initialization parameters.
      ServletContext getServletContext​()
      Returns a reference to the ServletContext in which this filter is running.
      void init​()
      A convenience method which can be overridden so that there's no need to call super.init(config).
      void init​(FilterConfig config)
      Called by the servlet container to indicate to a filter that it is being placed into service.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GenericFilter

        public GenericFilter​()

        Does nothing. All of the filter initialization is done by one of the init methods.

        Since:
        Servlet 4.0
    • Method Detail

      • getInitParameter

        public java.lang.String getInitParameter​(java.lang.String name)

        Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. See FilterConfig.getInitParameter(java.lang.String).

        This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig object.

        Specified by:
        getInitParameter in interface FilterConfig
        Parameters:
        name - a String specifying the name of the initialization parameter
        Returns:
        String a String containing the value of the initialization parameter
        Since:
        Servlet 4.0
      • getInitParameterNames

        public java.util.Enumeration<java.lang.String> getInitParameterNames​()

        Returns the names of the filter's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the filter has no initialization parameters. See FilterConfig.getInitParameterNames().

        This method is supplied for convenience. It gets the parameter names from the filter's FilterConfig object.

        Specified by:
        getInitParameterNames in interface FilterConfig
        Returns:
        Enumeration an enumeration of String objects containing the names of the filter's initialization parameters
        Since:
        Servlet 4.0
      • getFilterConfig

        public FilterConfig getFilterConfig​()

        Returns this servlet's ServletConfig object.

        Returns:
        FilterConfig the FilterConfig object that initialized this filter
        Since:
        Servlet 4.0
      • init

        public void init​(FilterConfig config)
                  throws ServletException

        Called by the servlet container to indicate to a filter that it is being placed into service. See Filter.init(javax.servlet.FilterConfig).

        This implementation stores the FilterConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).

        Specified by:
        init in interface Filter
        Parameters:
        config - the FilterConfig object that contains configuration information for this filter
        Throws:
        ServletException - if an exception occurs that interrupts the servlet's normal operation
        Since:
        Servlet 4.0
        See Also:
        UnavailableException
      • init

        public void init​()
                  throws ServletException

        A convenience method which can be overridden so that there's no need to call super.init(config).

        Instead of overriding init(FilterConfig), simply override this method and it will be called by GenericFilter.init(FilterConfig config). The FilterConfig object can still be retrieved via getFilterConfig().

        Throws:
        ServletException - if an exception occurs that interrupts the servlet's normal operation
        Since:
        Servlet 4.0