The solution of Chinese garbled problem in Java using AJAX

Source: Internet
Author: User

About Ajax using the Post method submitted to the Java background garbled problem, online there are many on the server side of the set request.setcharacterencoding ("Utf-8") this method to solve or to all the back-end coding are changed to UTF-8, However, it is not possible to use this method on your own project (later analysis may be the reason for using a coded filter). After a few days groping finally buttoned up:
1. Use escape or encodeURI method when submitting Ajax. Must be used 2 times
2, the backstage Java uses the Java.net.URLDecoder class the Decode method.

Front page Local code:
/**
* Marging is a JS pair like used to host the page part functions and parameters
* XMLHTTP is a rewritten Ajax-like, attribute paramstring the parameters sent to the server for post mode
* The GBK code used in the front and back page, using the coded filter
*/
xmlhttp.paramstring= "Mname=" +marging.mnames;
Parameter passes 2 times encodeURI Code
Xmlhttp.paramstring=encodeuri (xmlhttp.paramstring);
Xmlhttp.paramstring=encodeuri (xmlhttp.paramstring);
Send Save Request
var xmlreq=xmlhttp.gettextbypost ("mailmerginged.jsp");

Background page code:
/**
* When the Request.getparameter () function is invoked, the decoding process of the URI is automatically performed
* The decoding process built into the call can cause garbled characters to appear. And after the URI is encoded two times, the Request.getparameter () function
* Obtained is the original information URI encoded once the content. Re-use the controllable decoding function Java.net.URLDecoder.decode ()
* The original correct information can be solved.
*/
String Mname=java.net.urldecoder.decode (Request.getparameter ("Mname"), "UTF-8");

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.