Code within Web. XML:
<!--Character Set filters-- <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class><span style= "color: #ff0000;" >org.springframework.web.filter.CharacterEncodingFilter</span></filter-class> < init-param> <param-name><span style= "color: #ff0000;" >encoding</span></param-name> <param-value>UTF-8</param-value> </ init-param> <init-param> <param-name><span style= "color: #ff0000;" >forceEncoding</span></param-name> <param-value>true</param-value> </ Init-param> </filter>
<filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</ Url-pattern> </filter-mapping>
org.springframework.web.filter.CharacterEncodingFilter Source:
<span style= "font-family:arial, Helvetica, Sans-serif;" >package org.springframework.web.filter;</span>
Import Java.io.ioexception;import Javax.servlet.filterchain;import Javax.servlet.servletexception;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet 2.3/2.4 Filter That allows one to specify a character encoding for * requests. This is useful because current browsers typically does not set a * character encoding even if specified in the HTML page or Form. * * <p>this filter can either apply its encoding if the request does not * already specify an encoding, or enforce t His filter's encoding in any case * ("forceencoding" = "true"). In the latter case, the encoding would also be * applied as Default response encoding on Servlet 2.4+ containers (although * This'll usually is overridden by a full content type set in the view). * * @author Juergen Hoeller * @since 15.03.2004 * @see #setEncoding * @see #setForceEncoding * @see Javax.servlet.http.Htt pservletrequest#setcharacterencoding * @see javax.servlet.http.httpservletresponse#setcHaracterencoding */public class Characterencodingfilter extends Onceperrequestfilter {private String <span style= " Color: #ff0000; " >encoding</span>;p rivate boolean <span style= "color: #ff0000;" >forceencoding </span>= false;/** * Set the encoding to use for requests. This encoding'll be passed to a * {@link javax.servlet.http.httpservletrequest#setcharacterencoding} call. * <p>whether This encoding would override existing request encodings * (and Whether It'll be applied as default res Ponse encoding as well) * depends on the {@link #setForceEncoding "forceencoding"} flag. */public void setencoding (String encoding) {this.encoding = encoding;} /** * Set whether the configured {@link #setEncoding encoding} of this filter * are supposed to override existing request a nd response encodings. * <p>default is "false", i.e. does not modify the encoding if * {@link Javax.servlet.http.httpservletrequest#getcharac Terencoding ()} * 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. * <p>note that the response encoding would only is set on Servlet 2.4+ * containers, since Servlet 2.3 do not provid e a facility for setting * A default response encoding. */public void Setforceencoding (Boolean forceencoding) {this.forceencoding = forceencoding;} @Overrideprotected void Dofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain Filterchain) throws Servletexception, IOException {if (<span style= "color: #ff0000;" >this.encoding</span>! = null && (<span style= "color: #ff0000;" >this.forceEncoding</span> | | request.getcharacterencoding () = null) {<span style= "color: #ff0000;" >request.setcharacterencoding (this.encoding); </span>if (this.forceencoding) {<span style= "color:# ff0000; " >response.setcharacterencoding (this.encoding); </span>}}filterchain.dofilter (request, Response);}}
Characterencodingfilterclasses can be configured to help with character set conversion functionality, the parameterencodingused to specify the encoding type,Parametersforceencodingset totrue, Enforcementrequest.setcharacterencoding (this.encoding)and thereponse.setcharacterencoding (this.encoding)the methods in.
The study of Characterencodingfilter class