1. Use post to pass Parameters
sPara += sName + "=" + encodeURI(encodeURI(sValue)) + "&"; xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open( "POST" , sURL , true ); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(sPara);
Note: Here I used encodeuri twice, encodeuri (svalue ))
String providername = request. getparameter ("providername"); // when the post is passed, it must be providername = urldecoder. Decode (providername, "UTF-8") encoded by utf8 ");
Analysis: When the request. getparameter () function is called, The URI decoding process is automatically performed. The built-in decoding process during the call may cause garbled characters. After URI encoding twice, the request. getparameter () function obtains the content encoded once by the original information Uri. You can use the controllable decoding function java.net. urldecoder. Decode () to obtain the original correct information.
2. Use get to pass Parameters
The get method is better than the POST method, but it has character restrictions.
There are two methods
1. Add the Unescape method in the background for decoding
The front-end uses the Javascript escape method for Transcoding
public static String unescape (String src){ StringBuffer tmp = new StringBuffer(); tmp.ensureCapacity(src.length()); int lastPos=0,pos=0; char ch; while (lastPos<src.length()) { pos = src.indexOf("%",lastPos); if (pos == lastPos) { if (src.charAt(pos+1)=='u') { ch = (char)Integer.parseInt(src.substring(pos+2,pos+6),16); tmp.append(ch); lastPos = pos+6; } else { ch = (char)Integer.parseInt(src.substring(pos+1,pos+3),16); tmp.append(ch); lastPos = pos+3; } } else { if (pos == -1) { tmp.append(src.substring(lastPos)); lastPos=src.length(); } else { tmp.append(src.substring(lastPos,pos)); lastPos=pos; } } } return tmp.toString(); }
Ii. directly use encodeuri for Transcoding
Encodeuri for transcoding at the front end
Use request. getparameter ("name"); in the background to obtain the original data.
The possible cause is that when the request. getparameter () function is called, the process will automatically perform URI decoding once
These methods are mainly used to transmit data from the front-end to the back-end. Data transferred from the back-end to the front-end is not described.
Note: This article is mainly used for the accumulation of experience, so it is not written too carefully. If someone can see it, don't scold me if you don't understand it.