1.jsp头部<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
2.request.setCharacterEncoding("utf-8"); jsp页面提交请求的字符编码
3.在处理请求的jsp页面头部信息和第一条第二条一样
4. 在处理请求的页面进行转码
<%
String err = request.getParameter("err");
byte[] b = err.getBytes("iso-8859-1");
err = new String(b,"UTF-8");
response.setCharacterEncoding("utf-8");
out.println(err);
System.out.println(err);
%>
5.注意response.setContentType("text/html;charset=utf-8");和PrintWriter out = response.getWriter();的位置关系,
切记要将PrintWriter out = response.getWriter();放在response.setContentType("text/html;charset=utf-8");的后面,否则设置的编码将无效
问题搞定,不知道为什么,不转码的话就会出现乱码,原因还不清楚!现在好开心啊,感觉这个世界好美好啊,O(∩_∩)O哈哈~
下面是ajax验证输入用户名示例,ajax会将错误的中文信息传递给ajax.jsp页面,ajax.jsp接受信息并将这个中文信息在返回给ajax在页面显示
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>测试ajax</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlHttp;
var err = "";
function yanZheng() {
var e = document.getElementById("userid");
if (e.value.length < 6) {
return "*字符小于6位";
//document.getElementById("spanid").innerHTML = "<font color='red'>*字符小于6位</font>";
} else if (e.value.length > 12) {
return "*字符大于12位";
//document.getElementById("spanid").innerHTML = "<font color='red'>*字符大于12位</font>";
} else {
return "*正确";
}
}
function createXMLHttp(){
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
function vv(){
err = yanZheng();
xmlHttp = createXMLHttp();
url = "http://127.0.0.1:8080/Ajax/ajax.jsp?err="+encodeURI(err);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=callback;
xmlHttp.send(null);
}
function callback(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
document.getElementById("spanid").innerHTML = "<font color='red'>"+xmlHttp.responseText+"</font>";
err = "";
}
}
}
</script>
</head>
<body>
<center>
<br>
<br>
<br>
<form>
<table>
<tr>
<td align="right" width="200">
用户名:
</td>
<td align="left" width="400">
<input type="text" id="userid" name="username"
οnblur="vv()">
<span id="spanid"></span>
</td>
</tr>
<tr>
<td align="right" width="200">
密码:
</td>
<td align="left" width="400">
<input type="password" id="pwid" name="pw">
</td>
</tr>
<tr>
<td align="right" width="200">
<input type="button" value="提交">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
ajax.jsp 处理请求
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
String err = request.getParameter("err");
byte[] b = err.getBytes("iso-8859-1");
err = new String(b,"UTF-8");
response.setCharacterEncoding("utf-8");
out.println(err);
System.out.println(err);
%>