jquery的Ajax架構亂碼解決

來源:互聯網
上載者:User

1.下面代碼為伺服器端擷取請求資訊Servlet,真正用的時候可能就是struts等

package com.adu;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

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

 
public class AjaxServer extends HttpServlet {

    private static final long serialVersionUID = -1391282338046351379L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");  
      
        PrintWriter out = response.getWriter();
        商賬追收
        //擷取頁面jquery的url提交的資料,進行處理
//        String userName = request.getParameter("name");
//        String passWord = request.getParameter("password");
       
        String name = request.getParameter("name");
         String password = request.getParameter("password");
       
        //轉換字元集編碼,避免中文亂碼問題
        String userName = URLDecoder.decode(name,"UTF-8");
        String passWord = URLDecoder.decode(password,"UTF-8");
           
       
        //進行邏輯判斷,看輸入是否合法
        if(userName.length() != 0 && userName != null && passWord.length() != 0 && passWord != null){
           
            if(userName.equals("mawanli") && passWord.equals("123")){
               
                //out.print("Welcome* "+userName+" *visit my web!");
                out.print("歡迎* "+userName+" *訪問!");
                out.flush();
                out.close();
               
            }else{
               
                out.print("sorry please login again!");
//                out.print("請輸入正確的使用者名稱和密碼!");
            }   
        }
        else{
           
            out.print("please input right name and password!");
            //out.print("請重新登入!");
        }
         
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");
        this.doGet(req, resp);
    }

}

2.以下是頁面測試的代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!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" src="js/jquery-1.6.min.js"></script></head>
  <script type="text/javascript">
 
  //新增時間戳記,以防每次測試為不同的url,解決瀏覽器緩衝問題
function convertUrl(url) {  
     
      var timestamp = (new Date()).valueOf();  
      if (url.indexOf("?") >= 0) {  
       url = url + "&t=" + timestamp;  
      } else {   髮型123
         url = url + "?t=" + timestamp;  
     }  
       return url; 
    
  }
  //用encodeURI可以解決頁面傳值時的中文亂碼問題
  function validate(){
      var name = encodeURI(encodeURI($("#userName").val()));
      var password = encodeURI(encodeURI($("#passWord").val()));
      var url = "AjaxServer?name="+name+"&password="+password;
      url = convertUrl(url);
      
       $.get(url,null,function(data){
                               $("#result").html(data);
                       });
  }
 
 
  </script>
   
  <body>
   <div align="center">
   <div >
   <form action="#" method="get">
   <table>
   <tr><td>使用者名稱:</td><td><input type="text" name="userName" id="userName"/></td></tr>
   <tr><td>密&nbsp;&nbsp;&nbsp;&nbsp;碼:</td><td><input type="password" id="passWord"/></td></tr>
   <tr align="right"><td colspan="2"><input type="button" value="登入" onclick="validate()"/>
   <input type="reset" value="取消"/></td></tr>
   </table>
   </form>
  
   <div id="result"></div>
   </div>
   <hr width="600px" align="center" size="3" color="green"/>
  
  
   </div>
    
  </body>
</html>
<pre name="code" class="java"></pre>[align=left][/align]

相關文章

聯繫我們

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