用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时中文参数不能正常插入
展开
 我来答
david0248
2009-10-08
知道答主
回答量:2
采纳率:0%
帮助的人:3.1万
展开全部
你要分两步进行处理
1.前台对有汉字参数进行处理如name = encodeURIComponent(encodeURIComponent(name));
记住一定要进行两次编码
2.后台JAVA类对前台传过来的参数进行URL解码
String name = URLDecoder.decode(name, "utf-8");
这两步就可以了
xmlhttp.setRequestHeader( "Content-Type", "text/html;charset=GBK" );
你的这个写法是没有什么用的
敏放诚3
2009-10-08 · TA获得超过267个赞
知道小有建树答主
回答量:490
采纳率:0%
帮助的人:181万
展开全部
插入的时候不要进行 String name = new String(request.getParameter("uname").getBytes("iso-8859-1"),"GBK");
这种转换操作,直接去了试试
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
fylsh
2009-10-07 · TA获得超过950个赞
知道小有建树答主
回答量:1472
采纳率:0%
帮助的人:1075万
展开全部
在doGet方法中设置request的编码格式试试:
request.setCharacterEncoding("GBK");
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式