In js, you can use the escape ("") function to convert Chinese characters to unicode encoded strings, that is, the percent sign + u + letters/numbers. When the server side does not automatically decode it, we can write our own functions for transcoding, as shown below:
Public static String escape (String src ){
Int I;
Char j;
StringBuffer tmp = new StringBuffer ();
Tmp. ensureCapacity (src. length () * 6 );
For (I = 0; I <src. length (); I ++ ){
J = src. charAt (I );
If (Character. isDigit (j) | Character. isLowerCase (j)
| Character. isUpperCase (j ))
Tmp. append (j );
Else if (j <256 ){
Tmp. append ("% ");
If (j <16)
Tmp. append ("0 ");
Tmp. append (Integer. toString (j, 16 ));
} Else {
Tmp. append ("% u ");
Tmp. append (Integer. toString (j, 16 ));
}
}
Return tmp. toString ();
}
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 ();
}
/**
* @ Disc re-encode the string
* @ Param src
* @ Return
*/
Public static String isoToGB (String src ){
String strRet = null;
Try {
StrRet = new String (src. getBytes ("ISO_8859_1"), "GB2312 ");
} Catch (Exception e ){
}
Return strRet;
}
/**
* @ Disc re-encode the string
* @ Param src
* @ Return
*/
Public static String isoToUTF (String src ){
String strRet = null;
Try {
StrRet = new String (src. getBytes ("ISO_8859_1"), "UTF-8 ");
} Catch (Exception e ){
}
Return strRet;
}
Public static void main (String [] args ){
String tmp = "Chinese ";
System. out. println ("testing escape:" + tmp );
Tmp = escape (tmp );
System. out. println (tmp );
System. out. println ("testing unescape:" + tmp );
System. out. println (unescape ("% u6211 % u4eec "));
}
When js uses the escape () function to transcode the Chinese characters in the url, if the server cannot accept this Chinese parameter, use the encodeURI () function instead of the escape () function.