public class CharacterEncodingFilter extends OncePerRequestFilter
This filter can either apply its encoding if the request does not already specify an encoding, or enforce this filter's encoding in any case ("forceEncoding"="true"). In the latter case, the encoding will also be applied as default response encoding (although this will usually be overridden by a full content type set in the view).
setEncoding(java.lang.String)
,
setForceEncoding(boolean)
,
javax.servlet.http.HttpServletRequest#setCharacterEncoding
,
javax.servlet.http.HttpServletResponse#setCharacterEncoding
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
encoding |
private boolean |
forceRequestEncoding |
private boolean |
forceResponseEncoding |
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
CharacterEncodingFilter()
Create a default
CharacterEncodingFilter ,
with the encoding to be set via setEncoding(java.lang.String) . |
CharacterEncodingFilter(java.lang.String encoding)
Create a
CharacterEncodingFilter for the given encoding. |
CharacterEncodingFilter(java.lang.String encoding,
boolean forceEncoding)
Create a
CharacterEncodingFilter for the given encoding. |
CharacterEncodingFilter(java.lang.String encoding,
boolean forceRequestEncoding,
boolean forceResponseEncoding)
Create a
CharacterEncodingFilter for the given encoding. |
Modifier and Type | Method and Description |
---|---|
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request within a single request thread. |
java.lang.String |
getEncoding()
Return the configured encoding for requests and/or responses.
|
boolean |
isForceRequestEncoding()
Return whether the encoding should be forced on requests.
|
boolean |
isForceResponseEncoding()
Return whether the encoding should be forced on responses.
|
void |
setEncoding(java.lang.String encoding)
Set the encoding to use for requests.
|
void |
setForceEncoding(boolean forceEncoding)
Set whether the configured
encoding of this filter
is supposed to override existing request and response encodings. |
void |
setForceRequestEncoding(boolean forceRequestEncoding)
Set whether the configured
encoding of this filter
is supposed to override existing request encodings. |
void |
setForceResponseEncoding(boolean forceResponseEncoding)
Set whether the configured
encoding of this filter
is supposed to override existing response encodings. |
doFilter, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
@Nullable private java.lang.String encoding
private boolean forceRequestEncoding
private boolean forceResponseEncoding
public CharacterEncodingFilter()
CharacterEncodingFilter
,
with the encoding to be set via setEncoding(java.lang.String)
.setEncoding(java.lang.String)
public CharacterEncodingFilter(java.lang.String encoding)
CharacterEncodingFilter
for the given encoding.encoding
- the encoding to applysetEncoding(java.lang.String)
public CharacterEncodingFilter(java.lang.String encoding, boolean forceEncoding)
CharacterEncodingFilter
for the given encoding.encoding
- the encoding to applyforceEncoding
- whether the specified encoding is supposed to
override existing request and response encodingssetEncoding(java.lang.String)
,
setForceEncoding(boolean)
public CharacterEncodingFilter(java.lang.String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding)
CharacterEncodingFilter
for the given encoding.encoding
- the encoding to applyforceRequestEncoding
- whether the specified encoding is supposed to
override existing request encodingsforceResponseEncoding
- whether the specified encoding is supposed to
override existing response encodingssetEncoding(java.lang.String)
,
setForceRequestEncoding(boolean)
,
setForceResponseEncoding(boolean)
public void setEncoding(@Nullable java.lang.String encoding)
javax.servlet.http.HttpServletRequest#setCharacterEncoding
call.
Whether this encoding will override existing request encodings
(and whether it will be applied as default response encoding as well)
depends on the "forceEncoding"
flag.
@Nullable public java.lang.String getEncoding()
public void setForceEncoding(boolean forceEncoding)
encoding
of this filter
is supposed to override existing request and response encodings.
Default is "false", i.e. do not modify the encoding if
javax.servlet.http.HttpServletRequest#getCharacterEncoding()
returns a non-null value. Switch this to "true" to enforce the specified
encoding in any case, applying it as default response encoding as well.
This is the equivalent to setting both setForceRequestEncoding(boolean)
and setForceResponseEncoding(boolean)
.
public void setForceRequestEncoding(boolean forceRequestEncoding)
encoding
of this filter
is supposed to override existing request encodings.
Default is "false", i.e. do not modify the encoding if
javax.servlet.http.HttpServletRequest#getCharacterEncoding()
returns a non-null value. Switch this to "true" to enforce the specified
encoding in any case.
public boolean isForceRequestEncoding()
public void setForceResponseEncoding(boolean forceResponseEncoding)
encoding
of this filter
is supposed to override existing response encodings.
Default is "false", i.e. do not modify the encoding. Switch this to "true" to enforce the specified encoding for responses in any case.
public boolean isForceResponseEncoding()
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request within a single request thread.
See OncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterInternal
in class OncePerRequestFilter
ServletException
java.io.IOException