Coding setCharacterEncoding

  • effect pageEncoding = "UTF-8" is used for setting the encoding compiled JSP Servlet.
  • contentType = "text / html; charset = UTF-8" is the role of the server response specifies re-encoding encoded.
  • Effect request.setCharacterEncoding ( "UTF-8") is provided to client requests re-encoded encoded.
  • Effect response.setCharacterEncoding ( "UTF-8") is designated in response to the server re-encoded encoded. Meanwhile, the browser is re-encoded according to the parameters on the received data (or called decoding). So whether you set response.setCharacterEncoding in JSP ( "UTF-8") or response.setCharacterEncoding ( "GBK"), the browser can display Chinese correctly (provided that you send to encoded data browser is correct, For example, set correctly pageEncoding parameters, etc.).
  • For data transmission, the server in priority order response.setCharacterEncoding-contentType-pageEncoding of data to be transmitted is encoded.

  • Received data, to three cases. One is the data submitted by the browser with the URL directly, the other two are the data submitted with the form of GET and POST method.

    For the data submitted in the form POST, as long as the correct parameters in the JSP request.setCharacterEncoding received data, i.e. the client requests to be re-encoded code disposed browser coding, you can guarantee correct coding parameters obtained. Write readers may ask, how to get the browser encoding it? We mentioned above, and by default, please, the browser encoding is what you JSP page response value of the request response.setCharacterEncoding settings. So for data POST form submissions, response.setCharacterEncoding request.setCharacterEncoding to generate and submit the data obtained in the form of JSP page JSP page set to the same value.

    For data submitted URL and forms submitted GET method, the parameters set in the JSP request.setCharacterEncoding received data is not acceptable, because in Tomcat5.0, the default data using ISO- 8859-1 case where URL submitted and data submitted in the form GET method to re-encode (decode), and the form data without using the URL parameter submitted submitted GET method re-encoded (decoded). To fix the problem, you should set useBodyEncodingForURI or URIEncoding property in Connector label Tomcat configuration file, which useBodyEncodingForURI parameter indicates whether the form with the data and parameters for request.setCharacterEncoding URL submission submitted by GET method to re-encode in default case, the parameter is false (Tomcat4.0 default, this parameter is true); URIEncoding parameter specifies all the way GET requests (including URL data and submit forms submitted GET method) re-unified coding (decoding) encoding. URIEncoding and useBodyEncodingForURI difference is, URIEncoding is uniform re-encoding (decoding) the data of all the GET request, and re-encoding is useBodyEncodingForURI request.setCharacterEncoding parameters page response to the request data (decoded), different pages can have different re-encoding (decoding) encoding. So for data and URL submission form submitted GET method, you can modify the parameters for the browser URIEncoding code or modify useBodyEncodingForURI true, and request.setCharacterEncoding browser encoding parameter to obtain data in the JSP page.

Reprinted from: http://blog.csdn.net/fancylovejava/article/details/7700683

Published 74 original articles · won praise 23 · views 40000 +

Guess you like

Origin blog.csdn.net/qxhly/article/details/78991125