1,Content to be sent:
Format: XML; encoding: UTF-8
Ajax
Encoding: UTF-8 (req. getcharacterencoding (); read the client Encoding As UTF-8)
Servlet
Encoding: default (request bit encoding)
Result:
// Note: the output of tempcontent is not marked as normal. debug ("encoding =" + encoding); // encoding = utf-8log.debug ("tempcontent =" + tempcontent); // normal log. debug ("3 tempcontent =" + new string (tempcontent. getbytes ("gb2312"), "gb2312"); // normal log. debug ("5 tempcontent =" + new string (tempcontent. getbytes ("UTF-8"), "UTF-8"); // normal log. debug ("8 tempcontent =" + new string (tempcontent. getbytes ("UTF-8"), "ISO8859-1"); // normal
The following code generates the XML content:
function makeXmlTemp() { var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");xmlDoc.async = false;var p = xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");xmlDoc.appendChild(p);var root = xmlDoc.createElement('ocr');xmlDoc.appendChild(root); //alert(xmlDoc.xml);var str = xmlDoc.xml;str = str.replace('?>', ' encoding=\"utf-8\"?>');return str;}
The following code sends code for Ajax:
// Ajax request, using the synchronous method function ajaxrequest (URL, Param, method) {var XMLHTTP; var RS; var isie = true; If (window. activexobject) {XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); isie = true;} else if (window. XMLHttpRequest) {XMLHTTP = new XMLHttpRequest ();} Try {If (isie = false) {XMLHTTP. open ("get", URL, false); XMLHTTP. overridemimetype ("text/html; charset = gb2312"); XMLHTTP. send (null); // alert (XMLHTTP. re Extends setext); alert ("only supports ie! ");} Else {If (Method = 'post') {XMLHTTP. open ("Post", URL, false); XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8"); XMLHTTP. send (PARAM);} else {XMLHTTP. open ("get", URL, false); XMLHTTP. send (null);} If (XMLHTTP. readystate = 4) {If (XMLHTTP. status = 200 | XMLHTTP. status = 0) {return XMLHTTP. responsetext ;}}} catch (exception) {alert ('exception! '); // Alert ('exception:' + exception. message) ;}} var tempdat = makexmltemp (); // alert ('tempdat = '+ tempdat); $ I ('txttempfields '). value = tempdat; var tempurl = URL. geturi (); alert (tempurl); var Params = 'tempname = '+ tempname +' & tempcontent = '+ encodeuricomponent (tempdat); var echo = ajaxrequest (tempurl, Params, 'post'); alert (ECHO );
The servlet code is as follows:
void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String encoding = req.getCharacterEncoding(); log.debug("encoding=" + encoding); //req.setCharacterEncoding("utf-8"); try { String tempName = req.getParameter("tempName"); String tempContent = req.getParameter("tempContent"); String rv = SUCCESS; log.debug("tempName=" + tempName); log.debug("tempContent=" + tempContent); log.debug("1tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"gb2312")); log.debug("2tempContent=" + new String(tempContent.getBytes("UTF-8"),"gb2312")); log.debug("3tempContent=" + new String(tempContent.getBytes("gb2312"),"gb2312")); log.debug("4tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"UTF-8")); log.debug("5tempContent=" + new String(tempContent.getBytes("UTF-8"),"UTF-8")); log.debug("6tempContent=" + new String(tempContent.getBytes("gb2312"),"UTF-8")); log.debug("7tempContent=" + new String(tempContent.getBytes("ISO8859-1"),"ISO8859-1")); log.debug("8tempContent=" + new String(tempContent.getBytes("UTF-8"),"ISO8859-1")); log.debug("9tempContent=" + new String(tempContent.getBytes("gb2312"),"ISO8859-1")); tempContent = URLDecoder.decode(tempContent,"UTF-8"); log.debug("8tempContent=" +URLDecoder.decode(tempContent,"UTF-8"));} catch (Exception e) { e.printStackTrace();log.error("savaTempData failed.", e); }}
2. content to be sent:
Format: XML; encoding: gb2312
Ajax
Encoding: UTF-8 (req. getcharacterencoding (); read the client Encoding As UTF-8)
Servlet
Encoding: default (encoding not set for reqest)
Result:
Log. debug ("encoding =" + encoding); // encoding = utf-8log.debug ("tempcontent =" + tempcontent); // normal log. debug ("3 tempcontent =" + new string (tempcontent. getbytes ("gb2312"), "gb2312"); // normal log. debug ("5 tempcontent =" + new string (tempcontent. getbytes ("UTF-8"), "UTF-8"); // normal log. debug ("8 tempcontent =" + new string (tempcontent. getbytes ("UTF-8"), "ISO8859-1"); // normal
We can see that the correct decoding on the background is irrelevant to the XML encoding.
3. content to be sent:
Format: XML; encoding: gb2312
Ajax
Encoding: Bit setting (req. getcharacterencoding (); read client encoding is null)
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Servlet
Encoding: default (encoding not set for reqest)
Result:
Log. debug ("encoding =" + encoding); // encoding = NULL
Log. debug ("4 tempcontent =" + new string (tempcontent. getbytes ("ISO8859-1"), "UTF-8"); // normal
4. content to be sent:
Format: XML; encoding: gb2312
Ajax
Encoding: Bit setting (req. getcharacterencoding (); read client encoding is null)
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;);
Servlet
Encoding:
Req. setcharacterencoding ("UTF-8"); // or use the following statement // Req. setcharacterencoding ("ISO8859-1 ");
Result:
Same as 3. It can be seen that the encoding specified by req is not output normally and must be transcoded. It is irrelevant to whether encodeuricomponent () is used (once ).
5. content to be sent:
Format: XML; encoding: gb2312
Ajax
Encoding: GBK (req. getcharacterencoding (); read client Encoding As GBK)
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
Servlet
Encoding:
Req. setcharacterencoding ("UTF-8"); // or use the following statement // Req. setcharacterencoding ("ISO8859-1 ");
Result:
Cannot be decoded normally.
Summary
The experiment shows that the Ajax post data encoding has nothing to do with the data itself, and is irrelevant with the servlet encoding:
Req. setcharacterencoding ("UTF-8 ")
It is only related to the encoding used by Ajax and can only be UTF-8 (isn't it possible to use UTF-8 ?) :
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8 ");
When encapsulating Ajax functions, do not forget to specify the character set attribute: charset = UTF-8