Class AbstractFilter

  • All Implemented Interfaces:
    javax.servlet.Filter
    Direct Known Subclasses:
    NameableFilter

    public abstract class AbstractFilter
    extends ServletContextSupport
    implements javax.servlet.Filter
    Base abstract Filter simplifying Filter initialization and access to init parameters. Subclass initialization logic should be performed by overriding the onFilterConfigSet() template method. FilterChain execution logic (the Filter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) method is left to subclasses.
    Since:
    1.0
    • Field Detail

      • filterConfig

        protected javax.servlet.FilterConfig filterConfig
        FilterConfig provided by the Servlet container at start-up.
    • Constructor Detail

      • AbstractFilter

        public AbstractFilter()
    • Method Detail

      • getFilterConfig

        public javax.servlet.FilterConfig getFilterConfig()
        Returns the servlet container specified FilterConfig instance provided at startup.
        Returns:
        the servlet container specified FilterConfig instance provided at start-up.
      • setFilterConfig

        public void setFilterConfig​(javax.servlet.FilterConfig filterConfig)
        Sets the FilterConfig and the ServletContext as attributes of this class for use by subclasses. That is:
         this.filterConfig = filterConfig;
         setServletContext(filterConfig.getServletContext());
        Parameters:
        filterConfig - the FilterConfig instance provided by the Servlet container at start-up.
      • getInitParam

        protected String getInitParam​(String paramName)
        Returns the value for the named init-param, or null if there was no init-param specified by that name.
        Parameters:
        paramName - the name of the init-param
        Returns:
        the value for the named init-param, or null if there was no init-param specified by that name.
      • init

        public final void init​(javax.servlet.FilterConfig filterConfig)
                        throws javax.servlet.ServletException
        Sets the filter's filterConfig and then immediately calls onFilterConfigSet() to trigger any processing a subclass might wish to perform.
        Specified by:
        init in interface javax.servlet.Filter
        Parameters:
        filterConfig - the servlet container supplied FilterConfig instance.
        Throws:
        javax.servlet.ServletException - if onFilterConfigSet() throws an Exception.
      • onFilterConfigSet

        protected void onFilterConfigSet()
                                  throws Exception
        Template method to be overridden by subclasses to perform initialization logic at start-up. The ServletContext and FilterConfig will be accessible (and non-null) at the time this method is invoked via the getServletContext() and getFilterConfig() methods respectively.

        init-param values may be conveniently obtained via the getInitParam(String) method.

        Throws:
        Exception - if the subclass has an error upon initialization.
      • destroy

        public void destroy()
        Default no-op implementation that can be overridden by subclasses for custom cleanup behavior.
        Specified by:
        destroy in interface javax.servlet.Filter