今天测试密码输入了一串特殊字符后后台出来如下错误:
2010-9-311:44:56org.apache.tomcat.util.http.ParametersprocessParameters
警告:Parameters:Characterdecodingfailed.Parameterskipped.
java.io.CharConversionException:EOF
atorg.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:82)
或
java.io.CharConversionException:ishexdigit
atorg.apache.tomcat.util.buf.UDecoder.convert(UDecoder.java:82)
引起的原因:httpServletRequst对象中的键值对中包含:"%"
解决办法在ajax提交前先进行编码,在java后端在进行解码
ajax编码方法:value=encodeURIComponent(encodeURIComponent(str)).replace(/%20/g,"+");
java解码方法:
/**
* 转换ajax提交的表单编码 URLDecoder
* @param str
* @return
*/
public static String URLDecoder(String str){
if(str == null || str.isEmpty()){
return str;
}
try{
str = URLDecoder.decode(str, "UTF-8");
}catch(Exception e){
log.error("URLDecoder.decode转换前台传过来的汉字编码格式时候发生异常!", e);
}
return str;
}
一个测试输出的转换前和转换后结果:
request psw==========%40!%23%24%25%5E%26*()-%3D
decoder psw==========@!#$%^&*()-=