日期:2014-05-16  浏览次数:20329 次

新人求助~~~js传值到servlet取出乱码
add.js:

function CheckSubmit()
{
if(document.loginform.sname.value=="")
{alert("请输入姓名!");document.loginform.sname.focus();return false;}
document.location="http://localhost:8080/css/AddStuInfoServlet?sname="+document.loginform.sname.value;
return true;
}

addstu.jsp:
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<script type="text/javascript" src="add.js" charset="utf-8"></script>

AddStuInfoServlet:
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("sname");



输出的name就是乱码 js重新写改成utf-8编码也不行 jsp 和 html都是utf-8
请各位帮忙看下~~谢谢!!!

------解决方案--------------------
jsp、html、js的文件编码都是utf8吗?
------解决方案--------------------
文件编码统一
记事本 打开 另存为 编码 选相应的 试试
------解决方案--------------------
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
meta标签中http-equiv属性值加上引号试试。如果文件编码都正确,那就有可能是浏览器没有使用utf-8编码来解析页面,检查一下浏览器使用的编码,也可以在浏览器菜单中指定页面编码为UTF-8试试。
------解决方案--------------------
google了一下,貌似通过GET方式传递中文参数值比较容易碰到乱码问题,解决方法:
JScript code
//document.location="http://localhost:8080/css/AddStuInfoServlet?sname="+document.loginform.sname.value;
document.location="http://localhost:8080/css/AddStuInfoServlet?sname=" + encodeURIComponent (document.loginform.sname.value);

------解决方案--------------------
request.setCharacterEncoding("utf-8");
试试,我也是新手,不对勿怪
------解决方案--------------------
5楼正解
------解决方案--------------------
探讨
google了一下,貌似通过GET方式传递中文参数值比较容易碰到乱码问题,解决方法:

JScript code

//document.location="http://localhost:8080/css/AddStuInfoServlet?sname="+document.loginform.sname.value;
document.location="http://localh……

------解决方案--------------------
get方式传参会有中文乱码问题,可以处理但麻烦。建议使用post方式提交
如:
<form id="form1" method="post">
</form>
function submit(){
 document.forms["form1"].action="http://localhost:8080/css/AddStuInfoServlet";
 //document.forms["form1"].target="rightFrame";--有框架
 document.forms["form1"].submit();
}
------解决方案--------------------
首先在客户端 对url地址进行两次encodeURI(encodeURI(url));

然后在服务器端进行this.userName=java.net.URLDecoder.decode(this.userName,"UTF-8");进行转码。