Ajax 解決中文亂碼問題

來源:互聯網
上載者:User

CheckUserName.java的Servlet代碼如下:


package com.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckUserName extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //HttpServletResponse.setContentType("text/html:charset=utf-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        //擷取頁面參數
        String userName=request.getParameter("userName");
        //檢測參數是否與DB中相應資料相匹配
        if(userName==null||userName.length()==0){
            out.println("使用者名稱不可為空");
        }else{
            //校正操作
            if(userName.equals("tfq")){
                //返回頁面資料
                out.println("使用者名稱已經存在");
            }else{
                out.println("恭喜你!此使用者可用");
            }
        }
                
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        doGet(request, response);
    }

}

validata.js頁面代碼如下:

//驗證使用者是否存在
function validate() {

    // 1擷取文字框的內容
    // document.getElementById("userName"); dom的方式
    // Jquery的尋找節點的方式,參數中#加上id屬性值可以找到一個節點
    var obj = $("#userName");
    //解決中文亂碼  1:頁面端發出的資料作一次encodeURI(obj.val()),伺服器使用new String(userName.getBytes("iso8859-1","utf-8"))
    //解決中文亂碼  2:頁面端作兩次encodeURI(encodeURI(obj.val())),伺服器端用Strng userNamePara=URLDecoder.decode(userName,"utf-8");
    var userName = obj.val();
    //alert("userName: " + userName);
    // 2將擷取到的內容發送給伺服器的servlet
    // 調用jquery的XMLHTTPrequest對象get請求的封裝
    $.get("servlet/CheckUserName?userName=" + userName, null, function(data){
        // 3接收報務器返回的資料  4把返回的資料在div中顯示
        $("#showResult").html(data);
    });

}

checkUserName.html頁面代碼如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      <title>My JSP 'checkUserName.jsp' starting page</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">
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="js/validata.js" ></script>
  </head>
 
  <body>
    <center>
        <br />
        使用者名稱:<input type="text" id="userName" />
        <input type="button" value="校正" onclick="validate()" />
        <div id="showResult"></div>
    </center>
  </body>
</html>




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.