Solve the Chinese mess problem, the data sent by the page is made two times encodeURI
var name= "Zhang San";
encodeURI (encodeURI (name));
Background decoding:
Urldecoder.decode (name, "UTF-8");
URL encoding and two times encodeURI
When using the Address bar to submit query parameters, if not encoded, non-English characters will be encoded according to the operating system's character set to the server, the server will be decoded according to the configured character set, so if the inconsistency between the two will result in garbled characters.
The encodeURI function uses the UTF-8 to encode the URL, so if the server is decoding using the other encoding method will be garbled, the default server configuration of the decoding character set is not UTF-8, so most of the time the address bar to submit Chinese query parameters will produce garbled In this case, you can use encodeURI two times in a row to encode non-English characters on the client (primarily the browser) and then use Java.net.URLDecoder (String. ") on the server side. UTF-8 ") to get the correct Chinese.
If only one encodeURI, get the UTF-8 form of the URL, the server side through Request.getparameter () decoding query parameters (usually iso-8859-1) will get garbled.
If two times encodeURI, the first encoding is the UTF-8 form of the URL, the second encoding is still the UTF-8 form of the URL, but the effect is equivalent to the first UTF-8 encoding (now all converted to ASCII characters), Once again, iso-8859-1 encoding, because the English characters say UTF-8 encoding and ISO-8859-1 encoding the same effect. On the server side, the first decoding via Request.getparameter () first (possibly the character set of the gb2312,gbk,utf-8,iso-8859-1, which has no effect on the result) obtains the ASCII character, Then use UTF-8 for a second decoding, usually using the Java.net.URLDecoder ("", "UTF-8") method.
The process of encoding two times for two times is:
UTF-8 encoded->UTF-8 (iso-8859-1) encoding->iso-8859-1 decoding->utf-8 decoding, encoding and decoding process is symmetric, so there is no garbled.
Encodeurl function is mainly to do the URI to do transcoding, it is the default is to use the UTF-8 encoding.
UTF-8 encoded format: A Chinese character is made up of three bytes, each byte is converted to a 16 encoding, and a% number is added.
Assuming that the page-side input Chinese is a "medium", follow the steps below to decode
1. For the first time, encodeURI the byte array into [ -28,-72-83] by Utf-8 Way, iterating over the byte-code array, converting each byte into the corresponding 16-digit number, thus becoming [e4,b8,ad] and finally [?,?,?] There are no multibyte characters at this point, all of them are single-byte characters.
2, the second encodeURI, encoding, will be considered as an escape character, do not encode% after the character, will be the% encoded into%. Turns the array into [?,?,?] And then put the processed data [?,?,?] Sent to the server side,
When the application server calls the GetParameter method, the GetParameter method goes to the application server to request parameters
The application server initially obtained is sent to the [?,?,?], the application server will be UrlDecode operation of this data, the application server to decode this time, whether in accordance with UTF-8, or GBK, or ISO-8859, can get [?,?,?], because will be the% Parse to%. and return this value to the GetParameter method
3, again with UTF-8 decoding once, will get "medium".
Think about, if you do not encode two times, when the server automatically decode, if it is according to ISO-8859 to decode UTF-8 encoded things, it will appear garbled.
Easy to mix knowledge points:
Another mechanism for tracking the session is presented in the Java Servlet API, where the client browser does not support the Cookie,servlet container to rewrite the URL of the client request and add the session ID to the URL information.
The HttpServletResponse interface provides a way to rewrite the URL: public java.lang.String encodeurl (java.lang.String URL)
The implementation mechanism of this method is:
First, determine whether the current Web component is session-enabled, and if not, return the parameter URL directly.
Again determine whether the client browser supports cookies, if a cookie is supported, directly returns the parameter URL, if the cookie is not supported, the Session ID information is added to the parameter URL, and then the modified URL is returned.
Javaweb Request Chinese garbled