HTML uses the post method to submit Chinese content with garbled characters,

Source: Internet
Author: User

HTML uses the post method to submit Chinese content with garbled characters,

 

In today's example, the form is submitted using the post method. If there is a Chinese character, garbled characters are always displayed on the other page;

However, this error will not occur when you change the submission method to get.

For detailed errors, see the image and code below.

HTML code:

1 <! DOCTYPE html> 2 

Servlet code, part of the screenshot:

1 response. setContentType ("text/html; charset = UTF-8"); 2 3 PrintWriter out = response. getWriter (); 4 5 String title = "set Cookie instance"; 6 String docType = "<! DOCTYPE html> \ n "; 7 out. println (docType + 8 "

Error:

At first, I thought that the code block was not placed in the correct position. I put the above Code in the doPost for an interview. This error still occurred.

So how to use post to pass Chinese characters?

Search for information,

Post submission
In this case, response. setCharacterEncoding has an effect. If the value is null when response. setCharacterEncoding is not set, the iso-8859-1 is used by default for re-encoding (Decoding ).
The browser uses the encoding format of its own page as the starting encoding format to encode characters into bytes for transmission. tomcat will not interfere with the re-encoding (Decoding) format. If response. getCharacterEncoding is null, The iso-8859-1 is used by default for re-encoding (Decoding) into characters. If it is set, reencoding (Decoding) characters according to the set encoding format.

In the past, POST was used to transmit single-byte data. so the data encoding sent from POST is the single-byte data of the ISO-8859-1. therefore, there will be no garbled characters in English and numbers... in this case. filter and server. the settings in xml are invalid. of course, request. setCharacterEncoding () is also invalid because the setCharacterEncoding principle is the same as the filter;

 

The correct solution to the above problem should be:

1 String nameStr=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");

Change the ISO encoded content passed by post to the content in UTF-8 format, and then output.

 

See http://blog.csdn.net/sxyandapp/article/details/44623039.

 

Related Article

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.