First, let’s talk about the role of several encodings in JSP/ servlets. PageEncoding = “UTF-8”, contentType= “text/ HTML; Charset = utf-8 “, request. SetCharacterEncoding (” utf-8 “) and the response. SetCharacterEncoding (” utf-8 “), of which the first two can only be used in the JSP, The last two can be used in JSPS and servlets. 1, pageEncoding= “UTF-8” sets the encoding used when compiling JSPS into servlets. As we all know, JSPS are compiled into servlets on the server. PageEncoding = “UTF-8” tells the JSP compiler what encoding to use when compiling JSP files into servlets. In general, when a string defined internally in a JSP (defined directly in the JSP, rather than in data submitted from the browser) is garbled, much of it is caused by this parameter setting error. For example, specifying pageEncoding= “UTF-8” in your JSP file, which is saved in GBK encoding, will cause the strings defined inside the JSP to be garbled. In addition, as a function of the parameter is not specified in the JSP contentType parameters, also do not use the response. The setCharacterEncoding method, specify to recode the server response code. 2, contentType = “text/HTML. Charset =UTF-8″ specifies the encoding to recode the server response. Without using the response. SetCharacterEncoding method, use this parameter specifies to recode the server response code. This is what the server uses when it reencodes the data before sending it to the browser. 3, the request. SetCharacterEncoding (” utf-8 “) is set to recode the client request. This method specifies the encoding to use when re-encoding, or decoding, the data sent by the browser. 4, the response. SetCharacterEncoding (” utf-8 “) is designated to recode the server response code. This is what the server uses when it reencodes the data before sending it to the browser. Second, the browser would say how to send and receive data encoded response. SetCharacterEncoding (” utf-8 “) is designated to recode the server response code. The browser also re-encodes (or decodes) the data it receives based on this parameter. So in whatever you set in your JSP response. SetCharacterEncoding (” utf-8 “) or the response. SetCharacterEncoding (” GBK “), The browser will display Chinese correctly (provided that the data you send to the browser is correctly encoded, such as the pageEncoding parameter, etc.). The reader can do a experiment, set the response in the JSP. SetCharacterEncoding (” utf-8 “), the page is displayed in IE, menu, select the “view in IE (V)” a “coding (D) can see the” is “Unicode (utf-8)”, And the response is set in the JSP. SetCharacterEncoding (” GBK “), the page is displayed in IE, in IE menu, select the “view” a “(V) coding (D) can view is” in “simplified Chinese” (GB2312). Browser when sending data to the URL and parameters for URL encoding, the parameters of the Chinese, the browser is the use of the response. SetCharacterEncoding parameters for URL encoding. Take Baidu and GOOGLE for example. If you search for “Chinese characters” on Baidu, Baidu will encode it as “%BA%BA%D7%D6”. In the GOOGLE search “Chinese characters”, GOOGLE will be the code for “% E6 B1%89% AD E5% % % 97”, this is because baidu response. SetCharacterEncoding parameters for GBK, While GOOGLE’s response. SetCharacterEncoding parameters for utf-8. Browser in the receiving server used by the data and send data to the server code is the same, by default both the response of the JSP page. SetCharacterEncoding parameters (or contentType and pageEncoding parameters), we call it the browser encoding. Of course, you can change the browser encoding in Internet Explorer (by selecting “View (V)” “A” encoding (D) “from the Menu in Internet Explorer), but usually changing this parameter will result in garbled characters on an otherwise correct page. An interesting example is that when browsing GOOGLE’s home page in IE, modify the browser code to “Simplified Chinese (GB2312)”, at this time, the Chinese on the page will become gibberish, ignore it, enter “Chinese characters” in the text box, submit, GOOGLE will code it as “%BA%BA%D7%D6”, it can be seen, Browsers use browser encoding when they encode Chinese urls. Now that you know how the browser encodes data as it receives and sends data, let’s look at how the server encodes data as it receives and sends data. For sending data, the server response. SetCharacterEncoding – contentType – pageEncoding priorities, to encode data to be sent. For receiving data, there are three cases. One is data submitted directly by the browser using the URL, and the other two are data submitted using the GET and POST methods of the form. Because WEB servers handle these three approaches differently, we’ll use Tomcat5.0 as an example. Either way, if the parameter contains Chinese, the browser urL-encodes it using the current browser encoding. For POST way data submitted in the form, as long as the receiving data correctly in the JSP request. SetCharacterEncoding parameters, the client requests for recoding code set to browser code, can guarantee the parameters of the code is correct. How, readers may ask, do you get the browser code? We mentioned above, in the default case, please browser encoding is your response in the response to the request of the JSP page. SetCharacterEncoding set value. To POST the form submission of data, so the data of the JSP page request, and generate setCharacterEncoding to submit the form of a JSP page response. SetCharacterEncoding set to the same value. Data submitted for the URL and GET information in the form submit data, set in the receiving data of JSP request. SetCharacterEncoding parameters are not enough, because in Tomcat5.0, By default, ISO-8859-1 is used to re-encode (decode) the data submitted by URL and the data submitted by GET in the form, instead of using this parameter. To resolve this issue, set the useBodyEncodingForURI or URIEncoding property on the Connector tag of the Tomcat configuration file. The useBodyEncodingForURI parameter indicates whether or not to use the request. SetCharacterEncoding parameters on the URL to submit data and GET information in the form of data submitted to recode, by default, This parameter is false (the default is true in Tomcat4.0). The URIEncoding parameter specifies the uniform recoding (decoding) encoding of all GET requests, including data submitted by URL and data submitted by GET in forms. URIEncoding differs from useBodyEncodingForURI in that URIEncoding is a uniform re-encoding of all GET requests. UseBodyEncodingForURI is according to the response to the request of the requested page. SetCharacterEncoding parameters to the recoding of data (decoding), a different page can have different recoding (decoding) coding. Therefore, you can change the URIEncoding parameter to browser encoding or set useBodyEncodingForURI to true for URL submissions and GET submissions in the form. And in obtaining the data of the JSP page request. SetCharacterEncoding parameter set to the browser encoding. Here is a summary of how to prevent Chinese garbled characters when Tomcat5.0 is used as the WEB server. 1. For the same application, unified coding is recommended, and UTF-8 is recommended. Of course, GBK is also acceptable. 3. Set contentType=”text/ HTML “in all JSP/ servlets; Charset = utf-8 “or response. SetCharacterEncoding (” utf-8″), and indirectly, the coding of browser Settings. 4. For the request, you can use the filter or set in each JSP/Servlet request. SetCharacterEncoding (” utf-8 “). To change the default Tomcat configuration, you are advised to set the useBodyEncodingForURI parameter to true. You can also set the URIEncoding parameter to UTF-8 (not recommended because it may affect other applications). ———————————————— Copyright notice: This article is originally published BY CSDN blogger “Little Fat Pig who Loves learning”. It is subject to CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this statement. The original link: https://blog.csdn.net/heima201907/article/details/104532554