Encoding and decoding of request and response

 Since the decoding method cannot be changed by writing code, how to solve this problem? The solution is to de-encode, that is, we first decode the data obtained through functions such as request.getParameter("parameter"); ISO-8859-1, and then decode the obtained bytes through UTF-8. Got. Its code is: String str = new String(request.getParameter("parameter").getBytes("ISO-8859-1"), "UTF-8");

 

 

 

The origin of the Chinese argument code problem

        The reason for the problem of garbled characters is not only Chinese garbled characters, but also because different code tables are used for encoding and decoding. The reason why there is encoding and decoding is also because, no matter whether the data is in the process of transmission or storage, it runs in the form of bytes, so there is an encoding problem, and because of technological development, Various factors such as national conditions and other factors have led to the emergence of many coding methods in the world.

Two common encoding methods

        1ISO-8859-1 Latin code is used by the western buddies. There is no Chinese code at all, so ISO-8859-1 code is used. Needless to say, it will be messed up.

        2GBK, GB2312 This is China's national standard code, the current default code of browsers in China is GBK;

        3UTF-8 has too many encodings, which is not conducive to unification, so Unicode encoding of 2 or 4 bytes is generated. This encoding method supports the encoding of various countries in the world, so when writing programs, generally choose UTF-8. , Universal code, there is no way.

Three response garbled problem

        The response response object can output the response body to the client. Two response streams are provided in the response, namely the character response stream and the byte response stream. The character response stream can only be used to output characters, while the byte response stream can be used to output anything.

        1 character stream problem

        For the character response stream, the default encoding method of the character response stream is ISO-8859-1, so when we use the character stream to output Chinese, if we do not change the encoding method, it must be garbled, so how to solve Chinese What about the garbled problem?

        solution:

                a: Set the encoding method of the character stream of the response to UTF-8; its code is: response.setCharacterEncoding("UTF-8");

                b: Set the browser decoding to use the UTF-8 code table to decode; its code is: response.setHeader("content-type", "text/html;charset=UTF-8"); But after setting the response. setHeader("content-type", "text/html;charset=UTF-8"); In fact, there is no need to write response.setCharacterEncoding("UTF-8"); because the previous code not only sets the browser's decoding method, At the same time, the encoding method of the server is also set. There is also a convenient way of writing: response.setContentType("text/html;charset=UTF-8"); This code also sets the decoding method of the browser and also sets the encoding method of the server segment, so This way of writing code is generally used.

        2 byte stream problem

        For the byte response stream, the default encoding method is also ISO-8859-1, so the problem of Chinese garbled characters will also occur.

        solution:

                a, When encoding characters, specify the encoding method. The code is: response.getOutputStream().write("Beijing".getBytes("UTF-8"));

                b, and set the decoding mode of the browser at the same time. The code is: response.setHeader("content-type", "text/html;charset=UTF-8"); or response.setContentType("text/html;charset=UTF-8");

Four request garbled problem

        Let's start with the idea. We send data to the server, but how do we send it? In general, we have two ways, one is to write the request address and request parameters in the address bar; the other is to send data to the server by clicking on the form or hyperlink. Well, for the first method just mentioned, we will not deal with it here, because I think that generally no one does this. If he really does, let him handle it himself. So next, we will deal with the Chinese garbled problem of the second method, which can be divided into two types: get request and post request.

        1post request garbled problem

                For the post request, according to what we just mentioned, we send data to the server by clicking on the form or hyperlink, which means that we have already obtained the page sent from the server to the customer service. In this case, The encoding method of the page has generally been set to UTF-8, so we only need to set the decoding method on the server side to UTF-8. Its code is: request.setCharacterEncoding("UTF-8");

        2get request garbled problem

                For the get request method, in theory, we only need to tell the server to decode in UTF-8, but unfortunately, it does not allow us to write code to change the server's decoding method, only by modifying the server The specific operation is to find the server.xml file in the server's conf directory.

 

[html] view plain copy 1.<Connector port="8080" protocol="HTTP/1.1"   2.               connectionTimeout="20000"   3.               redirectPort="8443" /> 

Then add an attribute URIEncoding in it with a value of UTF-8. Although this method can solve the problem, we will not use it, because it is impossible for us to change this configuration when we write code on our own machine, and then put it in the future. When the project is handed over, by the way, big brother, remember to change the configuration when you go back. Do you think that's possible? So we don't do it this way.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325445942&siteId=291194637