Ajax异步校验验证码,servlet中有一个增强request解决中文的过滤器
求大神解决一个问题啊:如果在web工程中加一个过滤器(功能是对request的getparameter()方法进行增强,可以处理中文乱码),然后我使用ajax对验证码进行异步校验,但是每次从客户端传过去的中文验证码到servlet时使用request.getParameter("checkcode")接收到是乱码。。怎么解决呢。。如果去掉过滤器时按照普通接收方法是没问题的
代码如下:jsp页面:
$(function(){
$("#checkcode").blur(function(){
//当鼠标离开时获取输入的验证码
var codeInput = $("#checkcode").val();
//alert(codeInput);测试
$.post(
//路径
"${pageContext.request.contextPath }/checkcodeServlet",
//请求的数据
{"checkcode":codeInput},
//请求成功的执行的函数
function(data){
var isEqual = data.isEqual;
//alert(isEqual);测试
var codeInfo = "";
if(!isEqual){
codeInfo ="验证码不正确";
$("#codeInfo").css("color","red");
}
$("#codeInfo").html(codeInfo);
},
过滤器代码:增强request对象的getParameter()方法
//1.类型转换
/*final HttpServletRequest req = (HttpServletRequest) request;
//2.
HttpServletRequest requestEnhance = (HttpServletRequest) Proxy.newProxyInstance(
req.getClass().getClassLoader(),
req.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//获取getParameter()方法
String methodName = method.getName();
if("getParameter".equals(methodName)){
//获得客户端提交的信息-----中文乱码
String str = (String) method.invoke(req, args);
if(str!=null){
str = new String(str.getBytes("ISO8859-1"),"UTF-8");
return str;
}
}
return method.invoke(req, args);
}
});*/
//******记得放行******
chain.doFilter(request, response);
servlet代码:进行验证码校验
String codeClient = request.getParameter("checkcode");
System.out.println(codeClient);
//System.out.println(codeClient);---------如果通过过滤器的话---获取的客户端传过来的验证码是乱码
String codeServlet = (String) request.getSession().getAttribute("checkcode_session");
boolean isEqual = codeClient.equals(codeServlet)?true:false;