JSP XMLHttpRequest: dynamically refresh and process Chinese garbled characters

Source: Internet
Author: User

1. Add <script type = "text/JavaScript">
// Xmlhttp request without refreshing new requests
Var http_request = false;
Function createRequest (url)
{
Http_request = false;
If (window. XMLHttpRequest) {// Mozilla, Safari ,...
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType ){
Http_request.overrideMimeType ('text/html ');
}
} Else if (window. ActiveXObject) {// IE
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (e ){
Try {
Http_request = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request ){
Alert ('the XMLHTTP instance cannot be created! ');
Return false;
}
Http_request.onreadystatechange = alertContents;
Http_request.open ("POST", url, true );
Http_request.send (null );
}
2. Call -- call the request in js [CONTROL the url-mapping configured for servlet, passing parameters is the same as passing parameters in general URLs]
CreateRequest ('control' + '? Cmd = Listen ');
3. Handling [Chinese garbled characters] In servlet]
PrintWriter out;
Response. setContentType ("html/text"); // these five lines are required to output Chinese characters; otherwise, garbled characters
Response. setCharacterEncoding ("UTF-8 ");
Response. setHeader ("Cache-Control", "no-cache ");
Out = response. getWriter ();
Out. println ("sorry, the system is busy. Please try again later! "); // The returned response result is displayed on the page using alert ().

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.