Class CharacterEncodingFilter
- All Implemented Interfaces:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
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).
- Since:
- 15.03.2004
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
-
Constructor Summary
ConstructorDescriptionCreate a defaultCharacterEncodingFilter
, with the encoding to be set viasetEncoding(java.lang.String)
.CharacterEncodingFilter
(String encoding) Create aCharacterEncodingFilter
for the given encoding.CharacterEncodingFilter
(String encoding, boolean forceEncoding) Create aCharacterEncodingFilter
for the given encoding.CharacterEncodingFilter
(String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding) Create aCharacterEncodingFilter
for the given encoding. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doFilterInternal
(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread.Return the configured encoding for requests and/or responses.boolean
Return whether the encoding should be forced on requests.boolean
Return whether the encoding should be forced on responses.void
setEncoding
(String encoding) Set the encoding to use for requests.void
setForceEncoding
(boolean forceEncoding) Set whether the configuredencoding
of this filter is supposed to override existing request and response encodings.void
setForceRequestEncoding
(boolean forceRequestEncoding) Set whether the configuredencoding
of this filter is supposed to override existing request encodings.void
setForceResponseEncoding
(boolean forceResponseEncoding) Set whether the configuredencoding
of this filter is supposed to override existing response encodings.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Constructor Details
-
CharacterEncodingFilter
public CharacterEncodingFilter()Create a defaultCharacterEncodingFilter
, with the encoding to be set viasetEncoding(java.lang.String)
.- See Also:
-
CharacterEncodingFilter
Create aCharacterEncodingFilter
for the given encoding.- Parameters:
encoding
- the encoding to apply- Since:
- 4.2.3
- See Also:
-
CharacterEncodingFilter
Create aCharacterEncodingFilter
for the given encoding.- Parameters:
encoding
- the encoding to applyforceEncoding
- whether the specified encoding is supposed to override existing request and response encodings- Since:
- 4.2.3
- See Also:
-
CharacterEncodingFilter
public CharacterEncodingFilter(String encoding, boolean forceRequestEncoding, boolean forceResponseEncoding) Create aCharacterEncodingFilter
for the given encoding.- Parameters:
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 encodings- Since:
- 4.3
- See Also:
-
-
Method Details
-
setEncoding
Set the encoding to use for requests. This encoding will be passed into aServletRequest.setCharacterEncoding(java.lang.String)
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. -
getEncoding
Return the configured encoding for requests and/or responses.- Since:
- 4.3
-
setForceEncoding
public void setForceEncoding(boolean forceEncoding) Set whether the configuredencoding
of this filter is supposed to override existing request and response encodings.Default is "false", i.e. do not modify the encoding if
ServletRequest.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)
andsetForceResponseEncoding(boolean)
. -
setForceRequestEncoding
public void setForceRequestEncoding(boolean forceRequestEncoding) Set whether the configuredencoding
of this filter is supposed to override existing request encodings.Default is "false", i.e. do not modify the encoding if
ServletRequest.getCharacterEncoding()
returns a non-null value. Switch this to "true" to enforce the specified encoding in any case.- Since:
- 4.3
-
isForceRequestEncoding
public boolean isForceRequestEncoding()Return whether the encoding should be forced on requests.- Since:
- 4.3
-
setForceResponseEncoding
public void setForceResponseEncoding(boolean forceResponseEncoding) Set whether the configuredencoding
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.
- Since:
- 4.3
-
isForceResponseEncoding
public boolean isForceResponseEncoding()Return whether the encoding should be forced on responses.- Since:
- 4.3
-
doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException Description copied from class:OncePerRequestFilter
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread. SeeOncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
- Specified by:
doFilterInternal
in classOncePerRequestFilter
- Throws:
ServletException
IOException
-