Ajax当中中文处理的方法

中文处理:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。



例 2.1
<html>
<head>
<title></title>
<script language="JavaScript">
    var xmlrequest;

    function createXMLHttpRequest() {
         if(window.ActiveXObject){//ie
                alert("we are using microsoft ActiveXObject");
                xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if(window.XMLHttpRequest){
                xmlrequest = new XMLHttpRequest();
            }
    }
    createXMLHttpRequest();
    function postSend(value) {
        var uri = "servlet21";
        xmlrequest.open("POST", uri, true);
/*马克-to-win:through experiment, we find that we must use the
following statement, otherwise post can not work.
form-urlencoded是Ajax第一次编码,公认的Ajax是用的UTF-8编
码的,所以第二次编码是用的UTF-8编的码。*/
        xmlrequest.setRequestHeader("Content-Type",
                "application/x-www-form-urlencoded");
        xmlrequest.onreadystatechange = processResponse;
        xmlrequest.send("test=" + value);
    }

    function getSend(value) {
        var uri = "servlet21?test="+value ;
/*见上面POST所示,Ajax发送数据时,是经过两次编码的,
所以这里我也要两次编码。 */     
        uri=encodeURI(uri);
        uri=encodeURI(uri);
        xmlrequest.open("GET", uri, true);
        xmlrequest.onreadystatechange = processResponse;
        xmlrequest.send(null);
    }

    function processResponse() {
        if (xmlrequest.readyState == 4 && (xmlrequest.status == 200)) {
            var tex = xmlrequest.responseText;
//            alert(tex);
            document.getElementById("results").innerHTML = tex;
            return;

        }
    }
</script>
</head>
<body>
    <br />
    <br /> form1:
 
        <input id="test" name="test" type="text" size="90"> <br>
        <input type="button" value="GET发送"
            onclick='getSend(document.getElementById("test").value)'> <input
            type="button" value="POST发送"
            onclick='postSend(document.getElementById("test").value)'>
 
    <div id="results"></div>
</body>
</html>





package helloWorld;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet21 extends HttpServlet {
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String test=request.getParameter("test");
            test=java.net.URLDecoder.decode(test,"UTF-8");
            System.out.println("test is "+test);
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println(test);
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            String test=request.getParameter("test");
            System.out.println("test is "+test);
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println(request.getParameter("test"));
        }
}