SetCharacterEncodingFilter (Apache Tomcat 11.0.24 API Documentation)
Description: declaration: package: org.apache.catalina.filters, class: SetCharacterEncodingFilter

# Class SetCharacterEncodingFilter

java.lang.Object
org.apache.catalina.filters.FilterBase
org.apache.catalina.filters.SetCharacterEncodingFilter

All Implemented Interfaces:
  `Filter`

public class SetCharacterEncodingFilter extends FilterBase

Example filter that sets the character encoding to be used in parsing the incoming request, either unconditionally or only if the client did not specify a character encoding. Configuration of this filter is based on the following initialization parameters:

- encoding - The character encoding to be configured for this request, either conditionally or unconditionally based on the `ignore` initialization parameter. This parameter is required, so there is no default.
- ignore - If set to "true", any character encoding specified by the client is ignored, and the value returned by the `selectEncoding()` method is set. If set to "false, `selectEncoding()` is called only if the client has not already specified an encoding. By default, this parameter is set to "false".

Although this filter can be used unchanged, it is also easy to subclass it and make the `selectEncoding()` method more intelligent about what encoding to choose, based on characteristics of the incoming request (such as the values of the `Accept-Language` and `User-Agent` headers, or a value stashed in the current user's session).

- ## Field Summary

  ### Fields inherited from class FilterBase

  `sm`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `SetCharacterEncodingFilter()`
  Default constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `doFilter(ServletRequest request, ServletResponse response, FilterChain chain)`
  Select and set (if specified) the character encoding to be used to interpret request parameters for this request.
  `String`
  `getEncoding()`
  Returns the character encoding configured for this filter.
  `protected Log`
  `getLogger()`
  Returns the logger used by this filter.
  `boolean`
  `isIgnore()`
  Returns whether client-specified character encodings are ignored.
  `protected String`
  `selectEncoding(ServletRequest request)`
  Select an appropriate character encoding to be used, based on the characteristics of the current request and/or filter initialization parameters.
  `void`
  `setEncoding(String encoding)`
  Sets the character encoding to be configured for requests.
  `void`
  `setIgnore(boolean ignore)`
  Sets whether to ignore any character encoding specified by the client.

  ### Methods inherited from class FilterBase

  `init, isConfigProblemFatal`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface Filter

  `destroy`

- ## Constructor Details

  - ### SetCharacterEncodingFilter

    public SetCharacterEncodingFilter()
    Default constructor.

- ## Method Details

  - ### setEncoding

    public void setEncoding(String encoding)
    Sets the character encoding to be configured for requests.
    Parameters:
      `encoding` - the character encoding

  - ### getEncoding

    public String getEncoding()
    Returns the character encoding configured for this filter.
    Returns:
      the character encoding

  - ### setIgnore

    public void setIgnore(boolean ignore)
    Sets whether to ignore any character encoding specified by the client.
    Parameters:
      `ignore` - `true` to ignore client-specified encoding

  - ### isIgnore

    public boolean isIgnore()
    Returns whether client-specified character encodings are ignored.
    Returns:
      `true` if client encodings are ignored

  - ### doFilter

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    Select and set (if specified) the character encoding to be used to interpret request parameters for this request.
    Parameters:
      `request` - The servlet request we are processing
      `response` - The servlet response we are creating
      `chain` - The filter chain we are processing
    Throws:
      `IOException` - if an input/output error occurs
      `ServletException` - if a servlet error occurs

  - ### getLogger

    protected Log getLogger()
    Returns the logger used by this filter.
    Specified by:
      `getLogger` in class `FilterBase`
    Returns:
      the logger instance

  - ### selectEncoding

    protected String selectEncoding(ServletRequest request)
    Select an appropriate character encoding to be used, based on the characteristics of the current request and/or filter initialization parameters. If no character encoding should be set, return `null`.

    The default implementation unconditionally returns the value configured by the encoding initialization parameter for this filter.

    Parameters:
      `request` - The servlet request we are processing
    Returns:
      the encoding that was configured