js中有两个函数(escape,unescape)~!ajax提交表单:为了出现中文时不出现乱码需要经过escape进行转码通过用escape函数将中文转成如:“%u4E2D%u534E%u4EBA%u6C11%u5171%u548C%u56FD”的编码当要显示到页面时就要通过unescape函数进行解码 方可显示正常。问题是现在通过escape转码后 转码后的数据将会存入到后台 ,后台中却不知如何进行解码 ?请问高手们 在 java后台如何对用过js escape编码后的数据进行解码?谢谢
解决方案 »
- DRBD+heartbeat+mysql做双机热备
- Java 日文字符检索问题。
- 高分求解Tomcat异灵错误之socket write error
- java ee中的servlet是什么?应用中有什么作用
- 用List接收hibernate查询视图的结果时,其中有null怎么办?
- 关于连接MQ的问题
- java中的继承分配内存问题
- 用myeclipse布置struts报错org.apache.jasper.JasperException: This absolute uri (http://jakarta.apache.org/struts/tags-bean) canno
- 问关于x=x.replace('\r',' ');
- Eclipse、Tomcat、lomboz.213的问题
- 请教soap问题,不知道如何下手啊
- 数据转换问题
public 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 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();
}
java.net.URLDecoder.decode()
例如:<script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');</script>
2)、 进行url跳转时可以整体使用encodeURI
例如:Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度& ct=21");
3)、 js使用数据时可以使用escape
[Huoho.Com编辑]
例如:搜藏中history纪录。
4)、 escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下 escape,encodeURI,encodeURIComponent编码结果相同。
最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)escape不编码字符有69个:*,+,-,.,/,@,_,0-9,a-z,A-ZencodeURI不编码字符有82个:!,#,$,&,',(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a- z,A-ZencodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
javaScript中URL编码转换,escape() encodeURI() encodeURIComponent 另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在 javascript1.0版本就有。escape()不编码的字符:@*/+相对于使用escape方法,使用encodeURI方法会显得更专业一些。当你需要编码一整个URI的时候,你可以使用此方法,因为URI中的合法字符都不会被编码转换。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
encodeURI() 不编码的字符: ~!@#$&*()=:/,;?+''encodeURIComponent方法在编码单个URIComponent(指请求参数)应当是最常用的。需要注意到是字符’也是URI中的合法字符,所以也不会被编码转换。
encodeURIComponent()不编码的字符: ~!*()''JAVA后台相应处理:如果是用escape编码 则:通过自定义方法:
public 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();
}如果是用encodeURI编码 则:通过java.net.Decode.decode(request.getParameter("value"),"UTF-8")进行解码