Class HttpFilter

java.lang.Object
jakarta.servlet.GenericFilter
jakarta.servlet.http.HttpFilter
All Implemented Interfaces:
Filter, FilterConfig, Serializable

public abstract class HttpFilter extends GenericFilter

Provides an abstract class to be subclassed to create an HTTP filter suitable for a Web site. A subclass of HttpFilter should override doFilter(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.FilterChain).

Filters typically run on multithreaded servers, so be aware that a filter must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program.

Since:
Servlet 4.0
See Also:
  • Constructor Details

    • HttpFilter

      public HttpFilter()

      Does nothing, because this is an abstract class.

      Since:
      4.0
  • Method Details

    • doFilter

      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException

      The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain. There's no need to override this method.

      The default implementation inspects the incoming req and res objects to determine if they are instances of HttpServletRequest and HttpServletResponse, respectively. If not, a ServletException is thrown. Otherwise, the protected doFilter(jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, jakarta.servlet.FilterChain) method is called.

      Parameters:
      req - a ServletRequest object that contains the request the client has made of the filter
      res - a ServletResponse object that contains the response the filter sends to the client
      chain - the FilterChain for invoking the next filter or the resource
      Throws:
      IOException - if an input or output error is detected when the filter handles the request
      ServletException - if the request for the could not be handled or either parameter is not an instance of the respective HttpServletRequest or HttpServletResponse.
      Since:
      Servlet 4.0
      See Also: