JSP/Servlet ----- differences between charset and pageEncoding, and between jsp and servlet

Source: Internet
Author: User
Tags form post

JSP/Servlet ----- differences between charset and pageEncoding, and between jsp and servlet

I. Functions of several encodings in JSP/Servlet
In JSP/Servlet, encoding can be set in four places (as shown below). The first two can only be used in JSP, and the last two can be used in JSP and Servlet.
1. pageEncoding = "UTF-8": sets the encoding used when JSP is compiled into Servlet.
JSP is first compiled into Servlet on the server. PageEncoding = "UTF-8" is used to tell the JSP compiler the encoding used to compile the JSP file into a Servlet. Generally, many of the garbled characters defined in JSP (defined directly in JSP, rather than the data submitted from the browser) are caused by incorrect parameter settings. For example, your JSP file is saved in GBK encoding, while pageEncoding = "UTF-8" is specified in JSP, it will cause garbled characters defined inside JSP.
In addition, this parameter also provides a function that does not specify the contentType parameter in JSP or use the response. setCharacterEncoding method to re-encode the server response.


2. contentType = "text/html; charset = UTF-8": Specifies the encoding that recodes the server response.
If the response. setCharacterEncoding method is not used, this parameter is used to specify the re-encoding of the server response.


3. request. setCharacterEncoding ("UTF-8"): sets encoding for client requests.
This method is used to specify the encoding used to re-encode (or decode) the data sent by the browser.


4. response. setCharacterEncoding ("UTF-8"): Specifies the encoding that recodes the server response.
This encoding is used when the server re-encodes the data before sending the data to the browser.




2. How does the browser encode the received and sent data?
Response. setCharacterEncoding ("UTF-8") is used to specify the re-encoding of the server response. In addition, the browser uses this parameter to recode (or decode) the received data ). You can create an experiment and set response in JSP. setCharacterEncoding ("UTF-8"), when this page is displayed in IE, You can see "Unicode (UTF-8)" in IE's menu -- "View" -- "encoding )".
When the browser sends data, it will encode the URL and parameters. The browser also uses the response. setCharacterEncoding parameter to encode the Chinese characters in the parameters.
The encoding used by the browser to receive server data and send data to the server is the same, which is the response of the JSP page by default. the setCharacterEncoding parameter (or the contentType and pageEncoding parameters) is called browser encoding. Of course, you can modify the browser encoding in IE (modify it in IE's menu -- "View" -- "encoding), but normally, modifying this parameter will cause garbled characters on the correct page.



3. How does the server encode the received and sent data?
1. For data sending, the server will encode the data to be sent according to the priority of response. setCharacterEncoding-contentType-pageEncoding.
2. There are three situations for receiving data (data submitted by the browser directly using the URL, data submitted using the form GET method, and data submitted using the form POST method ). Various WEB servers have different processing methods. Take Tomcat5.0 as an example. No matter which method is used for submission, if the parameter contains Chinese characters, the browser uses the URL encoding of the current browser.
For the data submitted in POST mode in the form, as long as the request is correct in the JSP that receives the data. the setCharacterEncoding parameter sets the re-encoding of client requests to browser encoding (by default, browser encoding is response on the JSP page that responds to the request. setCharacterEncoding value) to get the correct parameter encoding.
For the data submitted by the URL and the data submitted by the GET method in the form, set the request in the JSP that receives the data. the setCharacterEncoding parameter does not work, because in Tomcat5.0, ISO-8859-1 is used by default to re-encode (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 ). To solve this problem, you must go to the Tomcat configuration file (server. in xml), set useBodyEncodingForURI In the Connector tag to indicate whether request is used. the setCharacterEncoding parameter re-encodes the data submitted by the URL and the data submitted by the GET method in the form. The default value is false. The default value is true in Tomcat4.0) or URIEncoding (specify the unified re-encoding (Decoding) attribute for all GET requests (including data submitted by URL and data submitted by GET in the form.




4. How to prevent Chinese garbled characters
1. The best unified encoding for the same application, recommended for UTF-8
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, either use a filter or set request. setCharacterEncoding ("UTF-8") in each JSP/Servlet "). To modify the default configuration of Tomcat, we recommend that you set useBodyEncodingForURI = "true" (request must be set before obtaining the parameter value. setCharacterEncoding ("UTF-8"), you can also set URIEncoding = "UTF-8" (it may affect other applications, so not recommended)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.