Java encoding and decoding garbled problem

Source: Internet
Author: User

Server Set Value (Chinese) to interface using two times encoding:

1 String pagejson=urlencoder.encode (Urlencoder.encode (str, "GBK"), "GBK"); 2 request.setattribute ("Pagejson", Pagejson);

Then in the interface and received the value to the background, with the Address bar direct URL:

Window.location.href= "... Test.do?method=test&pagejson=${pagejson}";

Decode directly in the background:

String Pagejson=urldecoder.decode (Request.getparameter ("Pagejson"), "GBK");

At this time the decoded value is garbled, the page Setup encoding format is also GBK, and not decoded before directly get the value of the parameter is garbled (in fact, no need to decode).

Should be uploaded to the server when the browser automatically decoded.

So add:

String pagejson=New string (Pagejson.getbytes ("iso-8859-1"), "GBK");

It's settled. In fact, although the page is encoded after the format, but is through window.location.href such a way to request to the background, so the browser automatically decode, with the usual general URL parameters submitted to the background is the same, the general request the default encoding format is Iso-8859-1, It is independent of the format of the page setup, and can be converted in normal format.

Java encoding and decoding garbled problem

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.