About the solution to the problem of submitting Chinese garbled characters in the get method

参考:
1、https://zhidao.baidu.com/question/552341375.html
2、https://blog.csdn.net/ty19921009/article/details/48712023
3、https://www.cnblogs.com/cdf-opensource-007/p/6337448.html

Submit Chinese garbled characters by get method

The solution can be considered from either of the following two aspects.

1.1. Server reason (tomcat)

  1. The text encoding when sent by the browser is consistent with the page encoding.
  2. If the encoding is not set for receiving requests in tomcat, ISO-8859-1 encoding is used by default.
  3. The page encoding uses UTF-8, and the get method naturally uses UTF-8 encoding, but the server receives no specified encoding format, and uses ISO-8859-1 by default.

solution:

  1. ISO-8859-1 encoding is a single-byte encoding, which can be converted to UTF-8 encoded Chinese using the following methods.

    String xx = new String(request.getParameter("key").getBytes("ISO-8859-1"), "UTF-8");`
  2. For tomcat, you can specify the encoding format as UTF-8 in the server.xml file .
    <Connector connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

1.2, front page encoding, background decoding

If you don't want to specify the encoding format on the server, you can transcode the Chinese URL when the page is submitted, and decode it in the background.

Front page:
var str=encodeURI(encodeURI("Here is the Chinese character string to be encrypted"));

Why encrypt twice here? The answer is that because of the first encryption, a string starting with % is generated, but in the browser, % is an escape character. When the browser submits to the server, the encrypted string % and % are added together. The encoding between the two bits is taken out and decoded, and then passed to the processing page, so encryption once is not enough, after two encryptions, only the correctly encrypted string can be obtained in the background before the decoding operation!

Backend page:
String result= URLDecoder.decode(request.getParameter("Get the encrypted string"), "utf-8");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607670&siteId=291194637