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. Then using the controllable decoding function Java.net.urldecoder.decode () can solve the original correct information.
Response.setheader ("CharSet", "gb2312");
See the original note as follows:
Using the Ajax in the use of get to read the page, responsetext inside the Chinese will probably appear garbled, this is because XMLHTTP in processing the return of the ResponseText, resposebody by default Utf-8 code, If the server is really utf-8 data stream when the Chinese character will be displayed correctly, and read the page encoding is GBK or gb2312 when the page will appear in Chinese garbled.
There are two solutions:
1. Add a header to the stream, indicating that the specific code stream is being sent:
PHP Tutorial: Header (' content-type:text/html;charset=gb2312 ');
ASP Tutorial: Response.Charset ("gb2312")
JSP Tutorial: Response.setheader ("CharSet", "gb2312");
2, the use of UTF-8 code to save HTML documents
Look at an Ajax JSP garbled solution
Ajaxpost.js
var myrequest; Variable to hold Request object
function Mysubmit () {
if (window.xmlhttprequest)
{
Myrequest = new XMLHttpRequest (); Standards-compliant Browsers
else if (window.activexobject)
{
Myrequest = new ActiveXObject ("Msxml2.xmlhttp"); For IE
}
var post= "name=" +document.getelementbyid ("Postval"). Value;
Post=encodeuri (POST);
Post=encodeuri (POST); The most important part, two times call encodeURI, is encoded two times
Myrequest.open ("Post", "Servlet/display", false);
Myrequest.setrequestheader ("ContentLength", post.length);
Myrequest.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Myrequest.send (POST);
The Var res=myrequest.responsetext;//receives the returned data
document.getElementById ("Display"). Innerhtml=res;
}
ajaxpost.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "gb2312"%>
<!doctype HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>my jsp ' ajaxpost.jsp ' starting page</title>
<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This are my page" >
<script type= "text/web Effects" src= "Js/ajaxpost.js" ></script>
<body>
<textarea rows= "Ten" cols= "id=postval></textarea>"
<br>
<input Type=button onclick= "Mysubmit ();" value= "Print" >
<div id= "Display" ></div>
</body>
Servlet
public void Dopost (HTTPS tutorial ervletrequest request, HttpServletResponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("GBK");
PrintWriter out = Response.getwriter ();
String Name=request.getparameter ("name");
Name=urldecoder.decode (name, "UTF8"); Post delivery, it must be encoded with UTF8, the URL can be set
System.out.println (name);
OUT.PRINTLN (name);
Out.flush ();
Out.close ();
}
Example Two
The initial page content is as follows (hello.jsp):
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%>
<%string path = Request.getcontextpath ();%>
<!doctype HTML PUBLIC "-//www.111cn.net//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= Wang Li-Mun &email=wallimn@sohu.com&bokee=http://down.111cn.net";
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>
/////////////////////////////////////////////////////////////////////////////////////