Ajax与服务器通信:发送请求和处理响应(2/4)

前端之家收集整理的这篇文章主要介绍了Ajax与服务器通信:发送请求和处理响应(2/4)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

要想充分发挥ajax的强大功能,这要求你向服务器发送一些上下文数据。

XMLHttpRequest对象的工作与以往惯用的HTTP技术(GET和POST)是一样的。

在下面的例子中,主要介绍这两种请求方式。
其中GET请求,我们向应用程序发送了3个参数:firstName,middleName,birthday.
其中资源URL和参数之间要用一个问号(?),问号后面就是名/值对。其采用name=value的形式,各个名值对之间用与(&)号分隔。
如:http://localhost:8080/yourapp/servlet/GetAndPostExample?firstName=Harry&middleName=Poter&birthday=20101010

服务器知道如何获取URL中的命名参数。

采用POST方法向服务器发送命名参数时,与采用GET时类似。POST方法也会将参数编码为名/值对。
这两种方式的主要区别在于,POST方法将参数串放在请求体中发送,而GET方法是将参数追加到URL中发送。

该示例有两个文件,其一为getandpost.html,另一个为servlet GetAndPostExample.java

详细代码如下:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>getAndPost.html</title>
  5. <Meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <script language="javascript" type="text/javascript">
  7. var xmlHttp;
  8. function createXMLHttpRequest(){
  9. if(window.ActiveXObject)
  10. {
  11. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  12. }
  13. else if(window.XMLHttpRequest)
  14. {
  15. xmlHttp = new XMLHttpRequest();
  16. }else{
  17. alert("浏览器不支持XMLHttpRequest对象");
  18. }
  19. }

  20. function createQueryString(){
  21. var firstName = document.getElementById("firstName").value;//document.getElementById("firstName").value;
  22. //alter(document.getElementById("firstName").value);
  23. var middleName = document.getElementById("middleName").value;
  24. var birthday = document.getElementById("birthday").value;
  25. //var birthday = document.getElementByID("birthday").value; //这句是错的!!!因为ID的d要小写!!!!!

  26. var queryString = "firstName="+ firstName + "&middleName="+ middleName + "&birthday=" + birthday;
  27. //alert(queryString);
  28. return queryString;
  29. }

  30. function doRequestUsingPOST(){
  31. createXMLHttpRequest();

  32. var url = "servlet/GetAndPostExample?timeStamp=" + new Date().getTime();
  33. var queryString = createQueryString();
  34. xmlHttp.open("POST",url,true);

  35. xmlHttp.onreadystatechange = handleStateChange;

  36. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
  37. xmlHttp.send(queryString);
  38. }
  39. function doRequestUsingGET(){

  40. createXMLHttpRequest();


  41. var queryString = "servlet/GetAndPostExample?";
  42. //alert(createQueryString());
  43. queryString = queryString + createQueryString() + "&timeStamp=" + new Date().getTime();
  44. alert("oo");
  45. xmlHttp.onreadystatechange = handleStateChange;
  46. xmlHttp.open("GET",queryString,true);

  47. xmlHttp.send(null);
  48. }
  49. function handleStateChange(){
  50. if(xmlHttp.readyState == 4){
  51. if(xmlHttp.status == 200){
  52. parseResult();
  53. alert("OK");
  54. }
  55. }
  56. }

  57. function parseResult(){
  58. var responseDiv = document.getElementById("serverResponse");
  59. if(responseDiv.hasChildNodes()){
  60. responseDiv.removeChild(responseDiv.childNodes[0]);
  61. }
  62. var responseText = document.createTextNode(xmlHttp.responseText);
  63. responseDiv.appendChild(responseText);
  64. }

  65. function test(){

  66. alert(document.getElementById("firstName").value);
  67. }
  68. </script>
  69. </head>

  70. <body>
  71. <h2>
  72. Enter your first name, middle name ,and birthday:
  73. </h2>

  74. <form action="#" id="form1" name="form1" >
  75. <table>
  76. <tbody>
  77. <tr>
  78. <td>
  79. First Name:
  80. </td>
  81. <td>
  82. <input type="text" id="firstName" value="Harry" />
  83. </td>
  84. </tr>
  85. <tr>
  86. <td>
  87. Middle Name:
  88. </td>
  89. <td>
  90. <input type="text" id="middleName" value="Poter" />
  91. </td>
  92. </tr>
  93. <tr>
  94. <td>
  95. Birthday:
  96. </td>
  97. <td>
  98. <input type="text" id="birthday" value="2020-8-9" />
  99. </td>
  100. </tr>
  101. </tbody>

  102. </table>
  103. <input type="button" value="Send parameters using GET"
  104. onclick="doRequestUsingGET();" />
  105. <input type="button" value="Send parameters using POST"
  106. onclick="doRequestUsingPOST()" />
  107. <input type="button" value="Test"
  108. onclick="return test()" />

  109. </form>
  110. <br />
  111. <h2>
  112. Server Response:
  113. </h2>
  114. <div id="serverResponse">
  115. 这里将会显示来自服务器端的相应信息
  116. </div>
  117. </body>
  118. </html>


  1. package ajax.study;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;

  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;

  8. public class GetAndPostExample extends HttpServlet {


  9. protected void processRequest(HttpServletRequest request,HttpServletResponse response,String method)
  10. throws ServletException, IOException{
  11. //set content type of the response to text/xml
  12. response.setContentType("text/xml");

  13. //get the user's input
  14. String firstName = request.getParameter("firstName");
  15. String middleName = request.getParameter("middleName");
  16. String birthday = request.getParameter("birthday");

  17. //Create the response text
  18. String responseText = "hello "+ firstName + " " + middleName +". Your birthday is " + birthday +"."
  19. + "[Method: " + method + "]";

  20. //write the response back to the brower
  21. PrintWriter out = response.getWriter();
  22. out.println(responseText);

  23. //close the writer
  24. out.close();


  25. }




  26. public void doGet(HttpServletRequest request, HttpServletResponse response)
  27. throws ServletException, IOException {
  28. //process the request in method processRequest
  29. processRequest(request,response,"GET");

  30. }

  31. public void doPost(HttpServletRequest request,"POST");
  32. }



  33. }

猜你在找的Ajax相关文章