Codecs involved in the Java web

Source: Internet
Author: User

The user initiates an HTTP request from the browser, where the encoding is URL, Cookie, Paramiter. After the server receives an HTTP request to resolve the HTTP protocol, where the URL, cookie and post form parameters to decode, the server may also need to read hard disk data (database, file), which may have coding problems. After the servlet has processed all the requested data, the data needs to be re-encoded via the socket to the user's requested browser and then decoded into text by the browser. These processes are represented by graphs as follows:

1.URL encoding and decoding

To verify how the browser encodes the URL, we select the Firefox browser and observe the actual content of the requested URL via the Httpfox plugin:

From the results, PathInfo is UTF-8 encoded , and QueryString is GBK encoding . As to why there is a%, the encoding specification of the URL is FRC3986: The browser-encoded URL encodes non-ASCII characters into 16 binary digits in some encoding format and adds "%" to each byte represented by the 16 binary.

From the above test results, the browser to PathInfo and QueryString encoding is not the same, different browsers on the PathInfo code may not be the same. For example, Chrome will turn to "Http://localhost:8080/%E4%B8%AD%E5%9B%BD?%E4%B8%AD%E5%9B%BD" for the request "http://localhost:8080/China", Here PathInfo and QueryString code are the same, are UTF-8 encoded.

2.HTTP Header encoding and decoding

When a client initiates an HTTP request, other parameters, such as cookies, Redirectpath, and so on, may be passed in the header in addition to the above URL, and the values of these user settings are likely to have encoding problems.

In Tomcat, Decoding the entries in the header is done when calling Request.getheader, and if the requested header entry does not decode, call Messagebytes's ToString method, which will use the default encoding from byte conversion from char Iso-8859-1 , and we can not set the header of the other decoding format, so if you set the header non-ASCII character decoding will certainly have garbled.

The same is true when we add headers, don't pass non-ASCII characters in the header, If it is necessary to pass the characters in Org.apache.catalina.util.URLEncoder encoding, and then add to the header, so that in the browser to the server is not lost information in the delivery, we can access these items and then follow the corresponding character set decoding.

3.POST encoding and decoding of forms

The Post form parameter is passed in the same way as QueryString, which is passed to the server via the body of HTTP. When we click the Submit button on the page, the browser will first encode the form's parameters according to the ContentType CharSet encoded format of the page and submit it to the server side. The server side is also decoded using the character set in ContentType. So the parameters submitted via the Post form are generally not problematic, and the character set encoding is set by ourselves.

In addition, for parameters of the Multipart/form-data type, which is the uploaded file encoding, the character set encoding defined by ContentType is also used. It is important to note that the upload file is a byte stream to the server's local temporary directory, this process does not involve character encoding, and the real encoding is when the content of the file is added to the parameters, if this can not be encoded with the default encoding iso-8859-1 will be encoded.

4.HTTP body encoding and decoding

When the user request of the resource server has been successfully obtained, the content will be returned to the client browser through response, the process must be encoded before the browser to decode, the browser according to the HTML <meta http-equiv= "Content-type" Content= "text/html; CHARSET=GBK "> in the CharSet to decode. If not defined, then the browser will use the default encoding to decode.

Access to the database is done through the client JDBC driver, using JDBC to access the data to be consistent with the built-in encoding of the data, which can be specified by setting the JDBC URL.

Encoding and decoding in 5.JS

The encoding of JS in the HTML file itself is consistent with the Content-type in the current page.

For the use of <script src= "script.js"/> type of JS file introduced, the browser will be the current page of the default character set parsing this JS file, if the external JS file encoding format and the current page encoding format consistent, Then you can not set this charset. However, if the encoding format of the Script.js file is inconsistent with the current page, the corresponding character set must be specified or garbled for non-ASCII characters.

6. Other places that need coding

In addition to the URL and parameter coding problems, there are many places on the server where coding may be required, such as reading XML, velocity template engine, JSP, or reading data from a database.

Reference: "In-depth analysis of Java Web Technology"

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.