开发项目用Ajax的Post方法提交信息到请求的servlet,输出遇到中文乱码,纠结了好几天,总结了网络上很多高手的心得,用了两步解决了乱码:
- 在JS端,对提交的字符串进行两次编码:
- <%@pagecontentType="text/html"pageEncoding="gbk"%>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Metahttp-equiv="Content-Type"content="text/html;charset=gbk">
- <title>JSPPage</title>
- <scripttype="text/javascript">
- varreq;
- functionvalidate(){
- //获取表单提交的内容
- varidField=document.getElementById("userName").value;
- //要发送的字符串数据
- varcontent="id=hello&message="+idField+"&say=我是帅哥";
- //重点:对content含中文字符的字符串进行两次编码
- content=encodeURI(content);
- varurl="validate.do";
- //创建一个XMLHttpRequest对象req
- if(window.XMLHttpRequest){
- //IE7,Firefox,Opera支持
- req=newXMLHttpRequest();
- }elseif(window.ActiveXObject){
- //IE5,IE6支持
- newActiveXObject("Microsoft.XMLHTTP");
- }
- req.open("post",url,true);
- req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- req.onreadystatechange=callback;
- //send函数发送请求,参数
- req.send(content);
- functioncallback(){
- //alert(req.readyState);
- //alert(req.status);
- if(req.readyState==4&&req.status==200){
- //alert(req.responseText);
- varcheck=req.responseText;
- show(check);
- functionshow(str){
- document.getElementById("info").innerHTML=str;
- </script>
- </head>
- <body>
- <h1>TestAjax</h1><p>
- <formaction=""method="get"name="form">
- <br>
- 输入用户名:
- <inputtype="text"size="10"maxlength="8"id="userName"name="name"onblur="validate()">
- <spanid="info"></span>
- 输入商品名:
- <inputtype="submit"name="refer">
- </form>
- </body>
- </html>
- 在servlet端,对接收的数据的进行一次解码:
- packagecom.model;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.util.Enumeration;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importjava.net.URLDecoder;
- /**
- *
- *@authorGavin
- */
- publicclassDoAjaxServletextendsHttpServlet{
- protectedvoidprocessRequest(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException{
- response.setContentType("text/html;charset=gbk");
- request.setCharacterEncoding("gbk");
- PrintWriterout=response.getWriter();
- try{
- response.setHeader("Cache-Control","no-store");
- response.setHeader("Pragma","no-cache");
- response.setDateHeader("Expires",0);
- //这里有个小问题:如果写成Stringput=null;将会输出一个null后面再加上增加的字符
- Stringput="";
- for(Enumeration<String>e=request.getParameterNames();e.hasMoreElements();){
- Stringh=(String)e.nextElement();
- Stringc=(String)request.getParameter(h);
- put+=c;
- //重点在这里,对接受到的信息进行解码
- StringputDecoded=java.net.URLDecoder.decode(put,"utf-8");
- System.out.println(pute);
- out.write("messageis:"+putDecoded);
- finally{
- out.close();
- @Override
- voiddoGet(HttpServletRequestrequest,250); line-height:18px"> processRequest(request,response);
- voiddoPost(HttpServletRequestrequest,85); font-weight:bold">publicStringgetServletInfo(){
- return"Shortdescription";
- }//</editor-fold>
- }