HTML uses post to submit Chinese content garbled Error resolution method

Source: Internet
Author: User
Tags set cookie

Today in the case of an example, the use of post to submit the form, if there is a Chinese language, in another page displayed, always garbled;

however, This error does not occur when you change the commit method to Get.

See the following image and code for detailed Errors.

HTML code:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "content-type"content= "text/html; charset=utf-8">5 <title>Test cookie settings and access</title>6 </Head>7 <Body>8     <formAction= "/testtomcat/setcookie"Method= "post">9Site Name:<inputtype= "text"name= "name"><BR>TenSite Url:<inputtype= "text"name= "url"><BR> one         <inputtype= "submit"value= "submit" /> a     </form> - </Body> - </HTML>

servlet code, Intercept Part:

1 Response.setcontenttype ("text/html;charset=utf-8");2         3 printwriter out=response.getwriter ();4         5 String title= "set Cookie instance";6String doctype= "<!DOCTYPE HTML>\ n ";7 out.println (doctype+8"<HTML>\ n "+9"<Head><title>"+title+"</title></Head>"+Ten"<Bodybgcolor=\ "#f0f0f0 \">\ n "+ one"<H1Align=\ "center\">"+title+"</H1>\ n "+ a"<ul>\ n "+ -"<Li><b>Site Name:</b>" -+ request.getparameter ("name") + "\ N</Li>" + the"<Li><b>Site URL:</b>" -+ request.getparameter ("url") + "\ N</Li>" + -"</ul>\ n "+ -"</Body></HTML>");

The error that occurred:

At first I thought when the code block placement is wrong, put the above code in the Dopost interview, or this error occurred.

So how to send the Chinese by post?

By looking up information,

Post mode submission
For this situation, Response.setcharacterencoding has an effect, and when no value is set to response.setcharacterencoding, it is re-encoded (decoded) by default with iso-8859-1.
The browser according to its own page encoding format as the starting encoding format, the characters are encoded into byte for transmission, to the Tomcat,tomcat do not interfere with the re-encoding (decoding) format. If Response.getcharacterencoding is null, it is re-encoded (decoded) into characters by default with iso-8859-1, and if set, re-encode (decode) the characters in the encoded format that is Set.

Post passes through Single-byte Data. so the data encoded by post is iso-8859-1 Single-byte Data. so the English and the numbers will not be garbled ... In this case, the settings in the filter and server.xml are not valid. of course request.setcharacterencoding () is also invalid because the principle of setcharacterencoding is the same as the filter;

The correct way to handle the above problem should be:

1 string namestr=new string (request.getparameter ("name"). getBytes ("iso-8859-1"), "UTF-8");

The iso-encoded content passed by post is changed to the content in UTF-8 format and then Output.

Refer to http://blog.csdn.net/sxyandapp/article/details/44623039.

HTML uses post to submit Chinese content garbled Error resolution method

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.