
用AJAX+SERVLET实现局部登陆,什么方法都试过了为什么jsp页面传递的中文参数仍然是乱码,望高手帮忙啊!!
开发工具:netbeans6.5+tomcat6.0其中jsp中的关键代码:varurl="Loginservlet?uname="+name+"&upword="+pa...
开发工具:netbeans6.5+tomcat6.0
其中jsp中的关键代码: var url="Loginservlet?uname="+name+"&upword="+pass;
//设置状态改变时所调用的函数
xmlhttp.onreadystatechange = stateChange;
xmlhttp.open("get",url,true);
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
//发送请求
xmlhttp.send(null);}
servlet中doget()方法: public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String name = new String(request.getParameter("uname").getBytes("iso-8859-1"),"GBK");
String pass = request.getParameter("upword");
System.out.println(name);
User user = new User();
user.setUname(name);
user.setUpword(pass);
String flag = "";
try {
flag = DAOFactory.getUserDAOinstance().login(user);
System.out.println(flag);
} catch (Exception e) {
}
if (!(flag.equals(""))) {
out.print(flag);//用户名密码正确就返回用户id
} else {
out.print("fail");//失败就返回字符fail
}
out.flush();
out.close();
}
注:因为一直只能对英文进行正确的判断,加断点在servlet的System.out.println(name);处对传入的参数调试进行输出,所得结果就成了"??????"
to fylsh:
加上request.setCharacterEncoding("GBK"); 仍然是"????"啊,原题中我已经写了filter类,而且也确实执行了过滤器。页面能够正常显示中文,也能够正常向数据库插入中文,就是做ajax时中文参数不能正常插入 展开
其中jsp中的关键代码: var url="Loginservlet?uname="+name+"&upword="+pass;
//设置状态改变时所调用的函数
xmlhttp.onreadystatechange = stateChange;
xmlhttp.open("get",url,true);
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
//发送请求
xmlhttp.send(null);}
servlet中doget()方法: public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String name = new String(request.getParameter("uname").getBytes("iso-8859-1"),"GBK");
String pass = request.getParameter("upword");
System.out.println(name);
User user = new User();
user.setUname(name);
user.setUpword(pass);
String flag = "";
try {
flag = DAOFactory.getUserDAOinstance().login(user);
System.out.println(flag);
} catch (Exception e) {
}
if (!(flag.equals(""))) {
out.print(flag);//用户名密码正确就返回用户id
} else {
out.print("fail");//失败就返回字符fail
}
out.flush();
out.close();
}
注:因为一直只能对英文进行正确的判断,加断点在servlet的System.out.println(name);处对传入的参数调试进行输出,所得结果就成了"??????"
to fylsh:
加上request.setCharacterEncoding("GBK"); 仍然是"????"啊,原题中我已经写了filter类,而且也确实执行了过滤器。页面能够正常显示中文,也能够正常向数据库插入中文,就是做ajax时中文参数不能正常插入 展开
3个回答
展开全部
你要分两步进行处理
1.前台对有汉字参数进行处理如name = encodeURIComponent(encodeURIComponent(name));
记住一定要进行两次编码
2.后台JAVA类对前台传过来的参数进行URL解码
String name = URLDecoder.decode(name, "utf-8");
这两步就可以了
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
你的这个写法是没有什么用的
1.前台对有汉字参数进行处理如name = encodeURIComponent(encodeURIComponent(name));
记住一定要进行两次编码
2.后台JAVA类对前台传过来的参数进行URL解码
String name = URLDecoder.decode(name, "utf-8");
这两步就可以了
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
你的这个写法是没有什么用的
展开全部
插入的时候不要进行 String name = new String(request.getParameter("uname").getBytes("iso-8859-1"),"GBK");
这种转换操作,直接去了试试
这种转换操作,直接去了试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在doGet方法中设置request的编码格式试试:
request.setCharacterEncoding("GBK");
request.setCharacterEncoding("GBK");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询