Java Web (ii) Response, request garbled problem solving in servlet

Source: Internet
Author: User

March does not lose weight, May sad, this is my present state, haha ~ Fitness, blog insisted.

--wh

One, the request parameter garbled problem 

    GET Request:

The parameters of the GET request are submitted after the URL, that is, in the request line,

          

          

Myservlet is a normal servlet, when the browser accesses it, using GET request to submit a name= xiaoming parameter value, in Doget to get the parameter value, and print to the console, found garbled

        The reason for garbled:

              Prerequisite Knowledge : Need to understand the Code table, encoding, decoding the meaning of these three nouns. Let me briefly say the usual,

Code table: is a rule, used to let us understand the language into a computer can understand the language of a rule , there are many is0-8859-1,gbk,utf-8,utf-16, such as a series of code tables, such as Gbk,utf-8, UTF-16 can identify a Chinese character, and if you want to identify English, you can use is0-8859-1 and other code tables.

Coding: Convert the language we understand into a language that computers can understand. This process is the function of coding.

Decoding: Translating computer-recognized languages into languages we can see. This process is the function of decoding.

Please refer to this blog post for details.

Here can only represent after a coding example, some programs will be a Chinese character or a letter with a different code table continuous coding several times, then the first encoding or the above function, the second encoding, is the computer can recognize the language into the computer can understand the language (the conversion rules are different), Then the decoding process, you have to go through two decoding, that is, the inverse of the code, the following example is a good illustration of this problem.

The browser is using the UTF-8 Code table, through the HTTP protocol transmission, the HTTP protocol only supports IS0-8859-1, to the server, the default is to use the Is0-8859-1 Code table, see figure

              

That is, three processes, two times the code, so we need to decode two times,

1, the browser will be "Xiao Ming" Use the UTF-8 Code table to encode (because xiaoming this is a Chinese character, so use can identify the Chinese code table, which we can manually set in the browser, if the use of the Code table can not identify Chinese, then will be garbled, because the Code table can not find the Chinese corresponding computer symbols, it may be used?? and other symbols), the encoding is 1234, and it is transmitted over the HTTP protocol.

2, in the HTTP protocol transmission, can only use the symbols represented in the Iso-8859-1 Code table, so will our original 1234 again to encode, this time using the iso-8859-1, get???? , and then transfer to the server

3, the server obtains this data is after two times encodes the data to obtain, therefore must follow the original code the process to decode, first UTF-8 code, then in the Iso-8859-1 code, then the decoding process, must be the first iso-8859-1 decoding, then in with UTF-8 decoding, This will give you the right data.????. GetBytes ("iso-8859-1");//First decoding, conversion to the computer can recognize the language, new String (1234, "UTF-8");//second decoding, conversion to the language we know

Resolving code

                

                

                

POST request:

The parameters of the post request are in the request body, relative to the GET request is much simpler, there is no HTTP protocol This step of the encoding process, so only on the server side, set the server decoding the Code table with the browser encoded Code table is the same, where the browser is using the UTF-8 Code table encoding, Then the server side will set the decoding Code table for UTF-8 is OK

Set server side to decode using UTF-8 code table

              request.setcharacterencoding ("UTF-8"); Command Tomcat to decode using the UTF-8 Code table instead of the default iso-8859-1.

So in many cases, the first sentence of the Dopost method is the code that prevents garbled characters when the request parameter is obtained.

     Summarize request parameter garbled problem

Get request and POST request way of Chinese garbled problem processing way different

Get: Request parameters in the request line, involving the HTTP protocol, manually solve the garbled problem, know the root cause of garbled, the right remedy, the principle is to do two times encoding, two decoding process

New String (Xxx.getbytes ("iso-8859-1"), "UTF-8");

Post: Request parameters in the request body, using the Servlet API to solve the garbled problem, the principle is to encode once decoding, command Tomcat to use a specific code table decoding.

Request.setcharaterencoding ("UTF-8");

            

Second , the response response back to the browser appears in Chinese garbled.           

Let's start by describing how the response object sends data to the browser. Two methods, a Getoutputstream, a getwrite.

Servletoutputstream Getoutputstream (); Gets the output byte stream. Provides write () and print () two output methods

PrintWriter Getwrite (); Gets the output character stream that provides write () and print () two output methods

The print () method is used at the bottom of the Write () method, and the equivalent of the print () method is to encapsulate the write () method, making it easier and quicker for developers to use it, and choose the appropriate print () method directly, regardless of how the bytes are converted.

1, Serveltoutputstream getoutputstream ();

Can not directly output Chinese, direct output Chinese will report abnormal,

                

The source code of the report exception

            

Solve:

Resp.getoutputstream (). Write ("hahaha, I want to output to the browser". GetBytes ("UTF-8"));

Will output the Chinese character first with UTF-8 encoding, without having tomcat to encode, so if the browser with a UTF-8 code table for decoding, then the correct output, if the browser is not UTF-8, then there will be garbled, So that's the key to see what the browser's code table, this is not very good, it is also important to note, is to use the Write (byte) method, because the print () method does not output a byte type method.

2, PrintWriter getwrite ();

The direct output of Chinese, will not be reported abnormal, but certainly will be reported abnormal, because the code table with ISO-8859-1 can not identify Chinese, the beginning is wrong, how to decode the code read useless

There are three ways to correctly output Chinese

1. Using the servlet API response.setcharacterencoding ()

Response.setcharac4terencoding ("UTF-8"); Let Tomcat encode the Chinese with UTF-8 that we want to respond to the browser, instead of using the default iso-8859-1, depending on whether the browser is using a UTF-8 code table that is as flawed as the one above

2. Notify Tomcat and browser to use the same Code table

Response.setcharacterencoding ("UTF-8"); Notify Tomcat to encode using the UTF-8 code table

Response.setheader ("Content-type", "text/html;charset=uft-8"); Manually set the response content to notify the browser to use Utf-8 for decoding.

Response.setcontenttype ("text/html;charset=uft-8"); Using the servlet API to inform the browser to use UTF-8 to decode, the underlying code is the previous line of code, a simple encapsulation.

              

              

              

So response in response, just notify Tomcat and browser to use the same Code table, that is, write two lines of code, then you can solve the problem of garbled response

Iii. Summary

In the above explanation always look very cumbersome, actually know the principle, very simple, now to summarize,

      Request garbled

GET Request:

It's been coded two times, so we're going to decode it two times.

First decode:xxx.getbytes ("iso-8859-1"); get yyy

Second decoding:new String (yyy, "Utf-8");

Continuous write:new String (Xxx.getbytes ("iso-8859-1"), "UTF-8");

POST request:

Once encoded, so it is only once decoded, using the servlet API request.setcharacterencoding ();

              request.setcharacterencoding ("UTF-8"); Not necessarily solve, depending on what the browser is using the Code table to encode, browser with UTF-8, then write UTF-8 here.

Response garbled

Getoutputstream ();

Using this byte output stream, can not directly output Chinese, will be abnormal, to output Chinese, the workaround is as follows

Resolution:Getoutputstream (). Write (Xxx.getbytes ("UTF-8")); Manual Chinese with UTF-8 Code table encoding, into byte transmission, into bytes, will not be reported abnormal, and Tomcat will not be encoded, because it has been encoded, so to the browser, if the browser is using the UTF-8 Code table decoding, then there will be no Chinese garbled, and vice versa , so this method, can not fully guarantee that Chinese is not garbled

Getwrite ();

Using the character output stream, can output Chinese directly, will not be abnormal, but will appear garbled.

Workaround: Notify Tomcat and the browser to use the same Code table.

                response.setcharacterencoding ("UTF-8"); Notify Tomcat to use UTF-8 Code table encoding

                Response.setcontenttype ("Text/html;charset=utf-8"); Notifies the browser to use UTF-8 decoding

Notifies the browser to use UTF-8 decoding the underlying principle of this method is this sentence: Response.setheader ("ContentType", "text/html;charset=utf-8");

Note: Both the Getoutputstream () and Getwrite () methods cannot be used at the same time, only one can be used at a time, or an exception is reported

          

Java Web (ii) Response, request garbled problem solving in servlet

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.