A. HTML form that uses get and post to submit information to the Servlet
1. <meta content = "text/html; charset = ISO-8859-1> the attribute in this HTML Tag is the encoding that tells the browser. In my opinion, you can write an HTML file and enter Chinese characters in the body. The charset value indicates the language used for HTML production. When the browser wants to display this HTML page, it will know what characters it uses to display it! There will be no garbled characters! Another function is page encoding!
For example, if it is set to charset = ISO-8859-1, you cannot enter Chinese when editing this page. If Big-5 is set here, you cannot use simplified Chinese when editing this page!
2. the request process can be summarized as follows: the client web page sends a request, the browser will automatically encode Chinese Characters Based on the character set used on the page (the meta page encoding is used here) and transmit it to the server. Then the server uses the character set for decoding. Then, the server uses character set encoding to encode the information to be returned, and then transmits it to the browser of the client for decoding! Here two decoding and encoding are involved.
3. Get Method Request page
The get method directly places the form information in the URL address when requesting information. The specific embodiment is that the request information is reflected in the query string parameters.
① The transmitted Chinese information is transmitted to the server through URL encoding (that is, the charset encoding format in Meta) in the page encoding format.
URL encoding rules:
URL encoding follows the following rules: each pair of name/value is separated by the &; operator; each pair of name/value from the form is separated by the = Operator. If the user does not enter a value for this name, the name still appears, but there is no value. Any special characters (that is, those that are not simple seven-digit ASCII, such as Chinese characters) will be encoded in hexadecimal notation with a percentile, of course, including
=, &;, And % special characters. In fact, URL encoding is a hexadecimal character ASCII code. However, there are some changes. You need to add "%" to the front ". For example, the ASCII code of "\" is, and the hexadecimal code of 92 is 5C, so the URL encoding of "\" is % 5c. What about the URL encoding of Chinese characters? For example, the ASCII code of "Hu" is-17670, The hexadecimal code is BAFA, And the URL code is "% Ba % Fa ".
②
String xx = request. getparameter ("XX ");
The server uses character sets for decoding, while the default character set for Tomcat is ISO-8859-1, so if this is to decode the passed hexadecimal code is certainly not correct (because the decoding and encoding must correspond to can refer to the http://wenku.baidu.com/view/14a91366caaedd3383c4d331.html ). If XX is output directly on the console, garbled characters are generated. How can we get the data we need?
Yes.
String xx = new string (request. getparameter ("text"). getbytes ("iso-8859-1"), "AA") (AA is the HTML page encoding format ). In this way, no garbled characters will appear in the console! (Note that the request. setcharacterencoding function does not work here .)
This is also troublesome every time, so you can change the default character set encoding of Tomcat. You can modify server. add the following statement in the XML file with uriencoding = "XX" (here XX is the commonly used HTML page encoding format) Configuration: <connector Port = "8080" protocol = "HTTP/1.1" connectiontimeout = "20000" redirectport = "8443" uriencoding = "gb18030"/>.
③. The server uploads the information to be transmitted to the client (browser) by encoding, And the browser obtains the information through decoding!
Here it involves a encoding and decoding, and on the server side, the default use of iso-8859-1 to encode into byte, wait for the browser, found that response information header no relevant encoding settings, the encoding format of the window system will be retrieved. The default Chinese system is GBK/gb2312. Therefore, the browser encoding format of the printed page is gb2312. If the response encoding format is set, the browser will decode the resolution according to the set character set even in the browser. The methods set here can be setcontenttype () and setcharacterencoding (). Response. setcharacterencoding set HTTP
Response encoding. If the encoding format is previously set using response. setcontenttype, the encoding format specified by response. setcharacterencoding overwrites the previous setting.
4. Post Method Request page
Post places the submitted data in the packet body of the http package, which is embodied in form data in the request information.
Reproduced to: http://www.cnblogs.com/iMouse-wu/p/3168659.html
Personal Summary: The second decoding and second Encoding Process in the request to the service response process.
POST method: the parameter encoding carried in the post body through the encoding set on the page. Server decoding. ISO-8859-1 is used by default in response
Get: URL encoding. url encoding is a hexadecimal ASCII code. However, there are some changes. You need to add "%" to the front ". For example, the ASCII code of "\" is, and the hexadecimal code of 92 is 5C, so the URL encoding of "\" is % 5c. What about the URL encoding of Chinese characters? For example, the ASCII code of "Hu" is-17670, The hexadecimal code is BAFA, And the URL code is "% Ba % Fa ".
Then decode the server.