1. 程式人生 > >Ajax和JSP傳值亂碼問題

Ajax和JSP傳值亂碼問題

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"
								onblur="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);
%>