Import java.io.ioexception;import javax.servlet.filter;import javax.servlet.filterchain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.servletrequest;import javax.servlet.servletresponse;public class Characterencodingfilter implements filter {protected string encoding = null ;p rotected filterconfig filterconfig = null;protected boolean ignore = true;public void init (Filterconfig filterconfig) throws servletexception { This.filterconfig = filterconfig;this.encoding = filterconfig.getinitparameter ("encoding") ; String value = filterconfig.getinitparameter ("Ignore");if (Value == null) this.ignore = true;else if (Value.equalsignorecase ("true")) this.ignore = true; else if (Value.equalsignorecase ("yes")) THIS.IGNORE&NBsp;= true;elsethis.ignore = false;} Protected string selectencoding (servletrequest request) {return (this.encoding);} Public void destroy () {this.encoding = null;this.filterconfig = null;} Public void dofilter (Servletrequest request, servletresponse response,filterchain chain) throws IOException, ServletException {if (ignore | | (request.getcharacterencoding () == null)) {String encoding = Selectencoding (Request);if (encoding != null) {///core code, rrequest.setcharacterencoding ( encoding);}} Chain.dofilter (Request, response);}}
1, request.setcharacterencoding
1) The encoding to use when setting the value obtained from the request.
2) The filter can only handle the Chinese garbled characters caused by post submission, and the Chinese code caused by the get submission is invalid.
Post Request Analysis: Form submission, the browser encodes the data according to the current page encoding scheme (the page encoding scheme can be set through the Pageencoding property, such as pageencoding= "UTF-8"), If you do not call the Request.setcharacterencoding set decoding scheme, then the default is to use "iso-8859-1" to decode the data, resulting in Chinese garbled.
Decoding mechanism for POST requests: When Request.getparameter is first invoked, Tomcat decodes all data according to the request.setcharacterencoding set-up decoding scheme, and subsequent calls to Request.getparameter do not Decoding, the request.setcharacterencoding must be called before the Request.getparameter is first called to decode the schema settings.
Get request Chinese garbled solution: need to be configured in Tomcat's server.xml, add attribute uriencoding= "UTF-8" to resolve get commit garbled problem. The code is as follows:
<connector connectiontimeout= "20000" port= "8080" protocol= "http/1.1" redirectport= "8443" URIEncoding= "UTF-8"/ >
Get request decoding mechanism: it is well known that the contents of get submissions will be displayed in the browser URL, data transmission in ASCII characters, in Tomcat, according to the Server.xml set in the uriencoding specified decoding scheme to decode. Because Tomcat knows that the data for the get commit is already decoded, the data is no longer decoded when Request.getparameter is called, so request.setcharacterencoding is naturally invalid.
2, Response.setcharacterencoding
Response.setcontenttype: Sets the encoding of the HTTP response and notifies the browser of the encoding used when it is displayed.
Response.setcharacterencoding: Sets the encoding of the HTTP response, if the encoding scheme was previously set with Response.setcontenttype, you can use response.setcharacterencoding to overwrite the previous settings , both of these methods must be called before Reponse commits or before response.getwriter. Otherwise, it is invalid.
This article is from the architect's path blog, so be sure to keep this source http://lizhuquan0769.blog.51cto.com/2591147/1694281
"Filter" Characterencodingfilter solve the Chinese garbled character caused by post submission of Javaweb project