Ajax發送GET和POST的處理

來源:互聯網
上載者:User
ajax  商務邏輯:頁面三個文字框,分別要求輸入姓,中間名和生日。兩個按鈕處理GET和POST請求,顯示輸入的結果。

    頁面:getAndPostExample.html

  <%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
用GET和POST發送頁面請求
</title>
<script type="text/javascript">
 //xmlHttpRequest對象
 var xmlHttp;

        //建立xmlHttpRequest對象
        function createXMLHttpRequest(){
         if (window.ActiveXObject){
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                else if(window.XMLHttpRequest){
                 xmlHttp = new XMLHttpRequest();
                }
        }
       
        //發送參數字串
        function createQueryString(){
         //得到姓
                var firstName = document.getElementById("firstName").value;
                //得到中間名
                var middleName = document.getElementById("middleName").value;
                //得到生日
                var birthday = document.getElementById("birthday").value;
               
                //構造請求字串
                var quertString = "firstName=" + firstName + "&middleName=" + middleName
                 + "&birthday=" + birthday;
               
                return quertString;
        }
       
        //處理GET請求
        function doRequestUsingGET(){
         createXMLHttpRequest();
               
                var queryString = "GetAndPostExample?";
                queryString = queryString + createQueryString() + "&timeStamp=" + new Date().getTime();
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.open("GET",queryString,true);
                xmlHttp.send(null);
        }
       
        //處理POST請求
        function doRequestUsingPOST(){
         createXMLHttpRequest();
               
                var url = "GetAndPostExample?timeStamp=" + new Date().getTime();
                var queryString = createQueryString();
               
                xmlHttp.open("POST",url,true);
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
                xmlHttp.send(queryString);
        }
       
        //回調方法
        function handleStateChange(){
         if (xmlHttp.readyState == 4){
          if (xmlHttp.status == 200){
                           //解析返回結果
           parseResults();
                        }
                }
        }
       
        //解析返回結果
        function parseResults(){
          var responseDiv = document.getElementById("serverResponse");
          if (responseDiv.hasChildNodes()){
           responseDiv.removeChild(responseDiv.childNodes[0]);
          }
          //返迴文本構造一個文本節點
          var responseText = document.createTextNode(xmlHttp.responseText);
          responseDiv.appendChild(responseText);
        }
</script>
</head>
<body>
<h1>
輸入你的姓,中間名,和生日:
</h1>
<table>
  <tbody>
   <tr>
            <td>姓:</td>
            <td><input type="text" id="firstName"/>
   </tr>
        <tr>
            <td>中間名:</td>
            <td><input type="text" id="middleName"/>
   </tr>
          <tr>
            <td>生日:</td>
            <td><input type="text" id="birthday"/>
   </tr>
  </tbody>
</table>

<form action="#">
  <input type="button" value="發送GET請求" />
  <br /><br />
 
  <input type="button" value="發送POST請求" />
</form>

<br />
<h2>
  伺服器響應:
</h2>
<div id="serverResponse">
</div>
</body>
</html>

伺服器端處理

GetAndPostExample.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class GetAndPostExample
    extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";

  //處理方法
  protected void processRequest(HttpServletRequest request,
                                HttpServletResponse response, String method) throws
      ServletException, IOException {
    //設定響應內容類別
    response.setContentType(CONTENT_TYPE);

    //得到參數
    String firstName = request.getParameter("firstName");
    String middleName = request.getParameter("middleName");
    String birthday = request.getParameter("birthday");
   
    //建立響應文本
    String responseText = "您好 " + firstName + " " + middleName + ". 您的生日是" + birthday
        + "." + " [傳參方法: " + method + "]";
   
    //傳回瀏覽器
    PrintWriter out = response.getWriter();
    out.println(responseText);
   
    //關閉寫出流
    out.close();
  }

  //Initialize global variables
  public void init() throws ServletException {
  }

  //處理GET方法
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    processRequest(request,response,"GET");
  }
 
  //處理POST方法
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    processRequest(request,response,"POST");
  }

  //Clean up resources
  public void destroy() {
  }
}

參考:Ajax基礎教程    當筆記吧 




相關文章

聯繫我們

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