The encoding method is as follows:
Pageencoding = "UTF-8", contenttype = "text/html; charset = UTF-8", request. setcharacterencoding ("UTF-8"), response. setcharacterencoding ("UTF-8 ")
1. pageencoding = "UTF-8" is used to set the encoding used when JSP is compiled into servlet.
Note: If the JSP file is saved in GBK encoding, but pageencoding = "UTF-8" is specified in JSP, it will cause garbled characters defined inside JSP.
2. contenttype = "text/html; charset = UTF-8" is used to specify the encoding of the server response.
3. Request. setcharacterencoding ("UTF-8") is used to set the re-encoding of client requests.
4. response. setcharacterencoding ("UTF-8") is used to specify the encoding for reencoding the server response.
Send data:
For data sending, the server follows the response. setcharacterencoding-contenttype-pageencoding: encode the data to be sent. The browser re-encodes (or decode) the data it receives based on this parameter ).
Receive data:
One is the data submitted by the browser directly using the URL, and the other two are the data submitted using the form get and post methods.
Regardless of the method used for submission, if the parameter contains Chinese characters, the browser uses the URL encoding of the current browser.
For data submitted in post mode in a form:
In the JSP that receives data, the request. setcharacterencoding parameter is correct and the encoding is specified. If not specified, ISO-8859-1 encoding is used by default and needs further processing.
It is worth noting that no getparameter () can be executed before setcharacterencoding () is executed (). This parameter is only valid for the POST method and invalid for the get method.
For the data submitted by the URL and the data submitted by the get method in the form:
In the JSP that receives data, Set Request. the setcharacterencoding parameter does not work, because by default, the ISO-8859-1 is used to recode (decode) the data submitted by the URL and the data submitted by the get method in the form ), this parameter is not used to re-encode (decode) the data submitted by the URL and the data submitted by the get method in the form ).
There are two solutions. One is to configure the middleware parameters. The other is pre-encoding of the characters that appear in it. In JavaScript, encodeuri (string) adopts browser encoding, which can be automatically recognized by JSP.
Summary:
1, for the same application, it is best to unified encoding, recommended for UTF-8, of course, GBK can also.
2. Set the pageencoding parameter of JSP correctly.
3. Set contenttype = "text/html; charset = UTF-8" or response. setcharacterencoding ("UTF-8") in all JSP/servlet to indirectly implement browser encoding settings.
4. For requests, you can use a filter or set request. setcharacterencoding ("UTF-8") in each JSP/servlet "). At the same time, to modify the default configuration of Tomcat, we recommend that you set the usebodyencodingforuri parameter to true, or you can set the uriencoding parameter to UTF-8 (which may affect other applications, so it is not recommended ).
Appendix:
Tomcat settings:
Set the usebodyencodingforuri or uriencoding attribute in the Tomcat ctor tag of the tomcat configuration file.
The usebodyencodingforuri parameter indicates whether to use request. the setcharacterencoding parameter re-encodes the data submitted by the URL and the data submitted by the get method in the form. By default, this parameter is false (the default value is true in tomcat4.0 );
The uriencoding parameter specifies the uniform re-encoding (Decoding) of all get requests (including data submitted by URL and data submitted by get in the form.
The difference between uriencoding and usebodyencodingforuri is that uriencoding uniformly recodes (decodes) the data of all get requests, while usebodyencodingforuri is based on the request on the page responding to the request. the setcharacterencoding parameter re-decodes data. Different pages can have different re-encoding (Decoding) codes.
Therefore, you can modify uriencoding to browser encoding or usebodyencodingforuri to true for URL-submitted data and get-submitted data in the form, and request data on the JSP page that obtains data. the setcharacterencoding parameter is set to browser encoding.
The encoding method is as follows:
Pageencoding = "UTF-8", contenttype = "text/html; charset = UTF-8", request. setcharacterencoding ("UTF-8"), response. setcharacterencoding ("UTF-8 ")
1. pageencoding = "UTF-8" is used to set the encoding used when JSP is compiled into servlet.
Note: If the JSP file is saved in GBK encoding, but pageencoding = "UTF-8" is specified in JSP, it will cause garbled characters defined inside JSP.
2. contenttype = "text/html; charset = UTF-8" is used to specify the encoding of the server response.
3. Request. setcharacterencoding ("UTF-8") is used to set the re-encoding of client requests.
4. response. setcharacterencoding ("UTF-8") is used to specify the encoding for reencoding the server response.
Send data:
For data sending, the server follows the response. setcharacterencoding-contenttype-pageencoding: encode the data to be sent. The browser re-encodes (or decode) the data it receives based on this parameter ).
Receive data:
One is the data submitted by the browser directly using the URL, and the other two are the data submitted using the form get and post methods.
Regardless of the method used for submission, if the parameter contains Chinese characters, the browser uses the URL encoding of the current browser.
For data submitted in post mode in a form:
In the JSP that receives data, the request. setcharacterencoding parameter is correct and the encoding is specified. If not specified, ISO-8859-1 encoding is used by default and needs further processing.
It is worth noting that no getparameter () can be executed before setcharacterencoding () is executed (). This parameter is only valid for the POST method and invalid for the get method.
For the data submitted by the URL and the data submitted by the get method in the form:
In the JSP that receives data, Set Request. the setcharacterencoding parameter does not work, because by default, the ISO-8859-1 is used to recode (decode) the data submitted by the URL and the data submitted by the get method in the form ), this parameter is not used to re-encode (decode) the data submitted by the URL and the data submitted by the get method in the form ).
There are two solutions. One is to configure the middleware parameters. The other is pre-encoding of the characters that appear in it. In JavaScript, encodeuri (string) adopts browser encoding, which can be automatically recognized by JSP.
Summary:
1, for the same application, it is best to unified encoding, recommended for UTF-8, of course, GBK can also.
2. Set the pageencoding parameter of JSP correctly.
3. Set contenttype = "text/html; charset = UTF-8" or response. setcharacterencoding ("UTF-8") in all JSP/servlet to indirectly implement browser encoding settings.
4. For requests, you can use a filter or set request. setcharacterencoding ("UTF-8") in each JSP/servlet "). At the same time, to modify the default configuration of Tomcat, we recommend that you set the usebodyencodingforuri parameter to true, or you can set the uriencoding parameter to UTF-8 (which may affect other applications, so it is not recommended ).
Appendix:
Tomcat settings:
Set the usebodyencodingforuri or uriencoding attribute in the Tomcat ctor tag of the tomcat configuration file.
The usebodyencodingforuri parameter indicates whether to use request. the setcharacterencoding parameter re-encodes the data submitted by the URL and the data submitted by the get method in the form. By default, this parameter is false (the default value is true in tomcat4.0 );
The uriencoding parameter specifies the uniform re-encoding (Decoding) of all get requests (including data submitted by URL and data submitted by get in the form.
The difference between uriencoding and usebodyencodingforuri is that uriencoding uniformly recodes (decodes) the data of all get requests, while usebodyencodingforuri is based on the request on the page responding to the request. the setcharacterencoding parameter re-decodes data. Different pages can have different re-encoding (Decoding) codes.
Therefore, you can modify uriencoding to browser encoding or usebodyencodingforuri to true for URL-submitted data and get-submitted data in the form, and request data on the JSP page that obtains data. the setcharacterencoding parameter is set to browser encoding.