Java coding Summary
I. Principles of practice
1. Save the page file in the same format as the statement.
If you declare <% @ page Language = "Java" pageencoding = "UTF-8" %>, the file is saved as UTF-8
2. Whether it is get, post or Javascript, the browser submits the request in the encoding format specified by the page file.
3. Javascript methods such as encodeuri and encodeuricomponent use UTF-8 format to encode data
Ii. HTTP and Servlet specifications
A. http protocol
1. Get according to the HTTP protocol, the URI and querystring in the get method are all iso-8859-1 encoding formats.
2, post method can use other encoding formats, such as UTF-8, the default is iso-8859-1
B. servlet specifications
1. servletrequest
Void setcharacterencoding (string env); this method can be used to specify the encoding format of data in the Request body.
Note: This method must be called before obtaining parameters and the input stream. Otherwise, it is invalid, that is, it is called before getparamater (string key) and getreader ().
2. servletresponse
Void setcharacterencoding (string charset); this method is used to set the encoding format of the container response output stream, but does not specify the response content format. Supported by servlet2.4 +
Void setcontenttype (string type); this method is used to specify the response content format and the response output stream format. The following two conditions are basically equivalent. The difference is that when a is used, the browser uses the default encoding, and B tells the browser to use the specified encoding:
A. setcharacterencoding ("UTF-8"); setcontenttype ("text/html ");
B. setcontenttype ("text/html; charset = UTF-8 ");
The preceding method can be used repeatedly to overwrite the previous settings, but before the output, for example, before calling getwriter ();
Iii. Server Status
Currently, many servers do not support void setcharacterencoding (string env) in servlet specifications. After all, foreign vendors use English languages and may not implement this method. Even if the modified method is called, still using default iso-8859-1 resolution
I personally tested the following server support:
A. APACHE-Tomcat-5.5.12
B. APACHE-Tomcat-6.0.18
Iv. Summary
A. Unified encoding method. For example, if UTF-8 is used, data is parsed using the specified encoding format on the server side (if the container does not support it, it is manually transcoded), and then the output format is specified.
B. Parameters in non-English format to be submitted must be transcoded before submission, for example, encodeuricomponent. After the server uses uridecoder decoding (UTF-8 format, or specify the output stream and content format as UTF-8, or convert to gb2312, return to iso-8859-1