/*********** I original, welcome reprint, Reprint please keep my information *************/
Author: wallimn
Email: wallimn@sohu.com
Blog: http://blog.csdn.net/wallimn
Time: 2006-11-15
/*********** I original, welcome reprint, Reprint please keep my information *************/
Today finally solved the problem of Ajax Chinese garbled, write an article to help friends with the same problem. My development environment: XP, Eclipse, using GB18030 encoding.
When encountered this problem, went to the internet to check a lot of articles, mentioned several solutions, such as: Total station UTF-8 code; Request headers encoded in Chinese; Use the escape function in JavaScript.
When using get method to submit data, Chinese problem is solved very well, setrequestheader ("Content-type", "text/html; encoding=gb18030") is OK. However, this method does not work in post mode. We all know that the get-way submission data has a length limit, and sometimes we have to submit the data using the Post method.
However, for the Post method, using the above several methods have been tested several times, the problem remains. I was depressed for several days.
Today to solve the problem, very simple, is to use escape (or encodeURI, two functions JavaScript function, the function is basically the same, you can check the relevant help), but to use two times, this is the key to solve the problem.
My example involves two pages, one is the initial page, the other is the AJAX request processing page.
The initial page content is as follows (hello.jsp):
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%string path = Request.getcontextpath ();%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>ajax Submit Page </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb18030 ">
<script type= "Text/javascript" >
function Justdo () {
var post= "Name= Zhang &email=wallimn@sohu.com&bokee=http://wallimn.bokee.com";
Post = encodeURI (POST);
Post = encodeURI (post);//two times, critical
var xmlobj = new ActiveXObject (' msxml2.xmlhttp ');
var URL = ' <%= path%>/page/act.jsp ';//filename needs to be adjusted to the appropriate location at test time?
Xmlobj.open (' Post ', url,true);
Xmlobj.setrequestheader ("Cache-control", "No-cache");
Xmlobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlobj.send (post);//Note: Post way, use this to send content?
}
</script>
<body>
<input type= "button" value= "Submit" onclick= "Justdo ()"/>
</body>
/////////////////////////////////////////////////////////////////////////////////////
The contents of the AJAX Request Processing page (act.jsp) are as follows:
/////////////////////////////////////////////////////////////////////////////////////
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%string path = Request.getcontextpath ();%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<% @page import= "Java.net.URLDecoder"%>
<title>ajax deal</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<body>
<%
Traverses the output parameter contents.
For (Enumeration E = Request.getparameternames (); e.hasmoreelements ();) {
String h = (string) e.nextelement ();
String v = request.getparameter (h);
String mm = Java.net.URLDecoder.decode (V, "UTF-8");
SYSTEM.OUT.PRINTLN ("Request parameter:" + H + "=" + mm);
}
%>
</body>
/////////////////////////////////////////////////////////////////////////////////////
Analysis: When the Request.getparameter () function is invoked, the decoding process of the URI is automatically performed, and the decoding process built into the call will cause garbled characters to appear. After the URI is encoded two times, the Request.getparameter () function gets the content that the original information URI encodes once. The Java.net.URLDecoder.decode () can be used to solve the original correct information by using the controllable decoding function.
The above analysis is purely personal opinion, I do not know whether it is correct.