web123456

SetCharacterEncoding method of request and response

Reprinted from:

/kong_lev/article/details/73071198

1、pageEncoding=”UTF-8”The function is to set the encoding used when JSP is compiled into Servlet.
2、contentType=”text/html;charset=UTF-8”The function is to specify the encoding the server responds to the browser.

JSP needs to go through "encoding" twice. The first stage will use pageEncoding, and the second stage will use utf-8to utf-8, the third stage is the web page produced by Tomcat, using contentType. 
 The first stage is jsp translation (translation) into.Java, it will read jsp according to the settings of pageEncoding, and the result is translated into a unified UTF-8JAVA source code (i.e..java), if the pageEncoding setting is wrong, or there is no setting, the Chinese garbled code comes out. 
 The second stage is from the source code (.java) Compile to bytecode file (.class), no matter what encoding scheme is used when writing JSP, the results after this stage are all UTF-8encoding java source code. 
 UTF for JAVAC-8encoding reads java source code and compiles it into UTF-8encoding binary code (i.e..class), this is the JVM's specification for the expression of constant numeric strings in binary code (java encoding). 
 The third stage is the loading and executing JAVA binary code from stage 2 by Tomcat (or its application Container). The output result is that it is seen on the client. At this time, the parameters contentType hidden in stage 1 and stage 2 are effective.

3. ("UTF-8") is used to set the encoding when requesting client and database values. If not specified, use iso-8859-1. (Only solve POST garbled code)

Tomcat can be modified by solving GET garbled codeserverURIEncoding property in .xml
 Or usestr = new String(str.getBytes("iso-8859-1"),"utf-8"); 

 
4. ("UTF-8") is to specify the encoding the server responds to the browser.

5. ("text/html;charset=utf-8") is used to specify the encoding the server responds to the browser. At the same time, the browser also reencodes (or decodes) the data it receives based on this parameter.

For sending data, the server encodes the data to be sent in the order of priority of —contentType—pageEncoding.

renew:

Get is the URL decoding method. The default decoding format is Tomcat8 encoding format. So the URL decoding is UTF-8.
Overwrite the request container decoding format
Post is the way to decode physical content. The default decoding format is the request encoding format. Nothing to do with Tomcat8 encoding format

The Response container in the tomcat server analyzes data with ISO8859-1 encoding by default. Therefore, if you need to parse Chinese in parameters, you need to set it

(“utf-8”);

Post to get the front desk data: (the request container is in gbk format by default)
(“utf-8”);
((“name”));

GET gets the foreground data: (There is no need to set the encoding format, the default is to follow the encoding format of the tomcat server)
((“name”));

GET POST transfers data to the front desk
(“utf-8”);
().write("I love you");