Ajax和JSP傳值亂碼問題

來源:互聯網
上載者:User

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);%>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.