In the previous blog, LZ describes the previous two scenarios (IO, memory) of the Java encoding and decoding operations, in fact, in both scenarios we only need to set the encoding and decoding process in the correct encoding and decoding method is generally not garbled. For those of us who are engaged in Java development, the most easily and most garbled place is the Web Part. First, let's see where the encoding conversion operation exists in Javaweb.
Encoding & Decoding
We can find out where the transcoding is in the Javaweb:
The user wants the server to send an HTTP request, where there is a URL, a cookie, a parameter, the encoded server accepts the HTTP request, parses the HTTP request, and decodes the URL, cookie, parameter. It is possible to read the database, local files, or other files on the network in the course of the server's business logic processing, all of which require encoding and decoding. When processing is complete, the server encodes the data and sends it to the client, which is decoded and displayed to the user. In this whole process involved in encoding decoding more places, wherein the most prone to garbled location is the server and the client to interact with the process.
Above the entire process can be summed up in this way, the page encoding data to the server, the server to obtain the data decoding operation, after a business logic processing after the final result encoding processing to the client, the client decoding display to the user. So I'm going to ask for the code & decoding of Javaweb.
Request
The client wants the server to send a request with nothing but four things:
1, direct access to the URL method.
2, page links.
3. Form Get submit
4. Form Post Submission
Url method
For the URL, if the URL is all in English, then there is no problem, if there is a Chinese will involve coding. How to encode? According to what rules to encode? And how to decode it? The following LZ will answer each! First look at the components of the URL:
In this URL, the browser will encode path and parameter. To better interpret the encoding process, use the following URL
http://127.0.0.1:8080/perbank/, I'm cm?name=, I'm cm.
Enter the above address into the browser URL input box, by viewing the HTTP header information we can see how the browser is encoded. The following is the encoding of IE, FIREFOX, chrome three browsers:
You can see that the major browsers encode "I Am" as follows:
|   |
Path section |
Query String |
|
Firefox |
E6 88 98 af |
E6 E6 E6 98 af |
|
Chrome |
E6 E6 98 AF |
< P>e6 E6 98 AF
|
|
IE |
E6 E6 98 AF |
CE D2 CA C7 |
Check the code of the previous blog for the path part of Firefox, Chrome, ie are using UTF-8 encoding format, for query string part of Firefox, Chrome adopted Utf-8,ie GBK. As to why it is added, this is because the encoding specification of the URL specifies that the browser encodes the ASCII character printable ASCII characters into 16 digits in some encoding format and then adds "%" to each byte that is represented by the 16 binary.
Of course, for different browsers, different versions of the same browser, different operating systems and other environments will result in different coding results, the table in one case, for the URL coding rules of any conclusion is premature. Because of the major browsers, each operating system URL URI, querystring encoding may be different, so the decoding of the server is bound to cause a lot of trouble, below we will have Tomcat, see how Tomcat is decoding the URL.
The URL to parse the request is in the Parserequestline method of Org.apache.coyote.HTTP11.InternalInputBuffer, which sets the byte[of the URL passed in to org.apache.co. Yote. The corresponding attribute of the Request. The URL here is still in byte format, and turning to Char is done in Org.apache.catalina.connector.CoyoteAdapter's Converturi method:
protected void Converturi (Messagebytes URI, request request) throws Exception {Bytechunk BC = Uri.getbytechunk (); int length = Bc.getlength (); Charchunk cc = Uri.getcharchunk (); Cc.allocate (length,-1); String enc = connector.geturiencoding (); Gets the URI decoding set if (enc! = null) {B2cconverter conv = Request.geturiconverter (); try {if (CONV = = null) {conv = new B2 Cconverter (ENC); Request.seturiconverter (CONV); }} catch (IOException e) {...} if (CONV! = null) {try {conv.convert (BC, CC, Cc.getbuffer (). Length-cc.getend ()); Uri.setchars (Cc.getbuffer (), CC.getstart (), cc.getlength ()); Return } catch (IOException e) {...} }}//Default encoding:fast conversion byte[] Bbuf = Bc.getb Uffer (); char[] Cbuf = Cc.getbuffer (); int start = Bc.getstart (); for (int i = 0; i < length; i++) {Cbuf[i] = (char) (Bbuf[i + start] & 0xff); } uri.setchars (cbuf, 0, length); }
From the above code, the decoding operation of the URI is to get the decoding set of connector first, which is in Server.xml
<connector uriencoding= "Utf-8" />
If not defined, it is parsed using the default encoding iso-8859-1.
For the query string section, we know that whether we commit by GET or post, all parameters are saved in parameters, Then we pass the Request.getparameter, the decoding work is the first time when the GetParameter method is called. Inside the GetParameter method, it calls the Org.apache.catalina.connector.Request Parseparameters method, which decodes the passed arguments. The following code is only part of the Parseparameters method:
Gets the encoded String enc = getcharacterencoding (); Gets the Charset boolean usebodyencodingforuri = Connector.getusebodyencodingforuri () defined in contenttype; if (enc! = null) { //If the set encoding is not empty, the setting is encoded as ENC parameters.setencoding (ENC); if (Usebodyencodingforuri) { //If Chartset is set, the decoding of QueryString is set to Chartset Parameters.setquerystringencoding (ENC); } } else { //Set default decoding mode parameters.setencoding (Org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING); if (Usebodyencodingforuri) { parameters.setquerystringencoding (org.apache.coyote.Constants.DEFAULT_ character_encoding); } }
From the code above, we can see that the decoding format of query string either takes the set Chartset or the default decoding format iso-8859-1. Note that the chartset of this setting is the contenttype defined in the HTTP header, and if we need to change the specified attribute to take effect, we need to configure the following:
<connector uriencoding= "UTF-8" usebodyencodingforuri= "true"/>
The above section details the encoding and decoding process for URL-mode requests. In fact, for us, more of our way is submitted by Form form.
Form get
We know that submitting data by URL is prone to garbled problems, so we are more inclined to form forms. When the user clicks on the Submit form, the browser will encode the data to the server with a more set encoding. Data submitted by get is stitched behind the URL (can be used as query String??). ) to commit, so the Tomcat server uriencoding during the decoding process. The Tomcat server is decoded according to the set uriencoding, and if not set, it is decoded using the default iso-8859-1. If we set the encoding to UTF-8 on the page, and the uriencoding setting is not or not set, then the server will be garbled when decoding. At this time we can generally get the correct data through the form of new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1″)," Utf-8″).
Form Post
For post, the encoding it uses is also determined by the page, which is contenttype. When I submit a form by clicking the Submit button on the page, the browser first encodes the parameters of the post form and submits it to the server according to the Ontenttype CharSet encoding format. On the server side is also used in the ContentType set of character sets to decode (here and get method is different), this is the post form submitted by the parameters are generally not garbled problem. Of course, this character set encoding we can set ourselves: Request.setcharacterencoding (CharSet).
Java Chinese garbled solution (vi) encoding and decoding in-–javaweb