posts - 36, comments - 30, trackbacks - 0, articles - 3

          Ajax學習二

          Posted on 2009-11-15 09:28 笑看人生 閱讀(255) 評論(0)  編輯  收藏 所屬分類: Web開發技術
          Ajax中的XMLHttpRequest對象提供了兩個屬性來訪問服務器端相應。

          • responseText:將相應作為一個字符串返回;(系列一中已經介紹)
          • responseXML:將相應作為一個XML對象返回;(本系列中介紹)
          本節要介紹的內容,很多人應該比較熟悉,比如在網上注冊時,在“省”列表框中選擇不同的省份,對應的“市”列表框中出現該省的所有市,這個過程,不用刷新整個頁面。

          要實現這個功能,只須修改一下系列一中的index.jsp和AjaxServlet.java這兩個文件。

          index.jsp

          <%@ page language="java" contentType="text/html; charset=UTF-8"
              pageEncoding
          ="UTF-8"%>
          <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
          <html>
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
          <title>Insert title here</title>
          </head>

          <script language="javascript">
              
          var xmlHttp;
              
          function refresh() {
                  xmlHttp 
          = createXMLHttpRequest();
                  
          var url = "AjaxServlet?province="
                          
          + document.getElementById("province").value;
                  xmlHttp.open(
          "GET", url);
                  xmlHttp.onreadystatechange 
          = handleStateChange;
                  xmlHttp.send(
          null);
              }

              
          function handleStateChange() {
                  
          if (xmlHttp.readyState == 4) {
                      
          if (xmlHttp.status == 200) {
                          updateCity();
                      }
                  }
              }

              
          function updateCity() {
                  clearCity();
                  
          var city = document.getElementById("city");
                  
          var cities = xmlHttp.responseXML.getElementsByTagName("city");        
                  
          for(var i=0;i<cities.length;i++){
                      option 
          = document.createElement("option");
                      option.appendChild(document.createTextNode(cities[i].firstChild.nodeValue));
                      city.appendChild(option);                        
                  }
              }

              
          function clearCity() {        
                  
          var city = document.getElementById("city");
                  
          while(city.childNodes.length > 0) {
                      city.removeChild(city.childNodes[
          0]);
                  }                    
              }
              
              
          function createXMLHttpRequest() {
                  
          if (window.ActiveXObject) {
                      xmlHttp 
          = new ActiveXObject("Microsoft.XMLHTTP");
                  } 
          else if (window.XMLHttpRequest) {
                      xmlHttp 
          = new XMLHttpRequest();
                  }
                  
          return xmlHttp;
              }
          </script>

          <body>
          <form action="#"><select id="province" onchange="refresh()">
              
          <option value="">Select One</option>
              
          <option value="jiangsu">Jiang Su</option>
              
          <option value="zhejiang">Zhe Jiang</option>
          </select> <br>
          <br>
          <br>
          <select id="city"></select></form>
          </body>

          </html>

          AjaxServlet.java

          package servlet;

          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 AjaxServlet extends HttpServlet {

              
          private static final long serialVersionUID = 7032718233562299325L;

              @Override
              
          protected void doPost(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "POST");
              }

              @Override
              
          protected void doGet(HttpServletRequest req, HttpServletResponse response)
                      
          throws ServletException, IOException {
                  processRequest(req, response, 
          "GET");
              }

              
          private void processRequest(HttpServletRequest req,
                      HttpServletResponse response, String method) 
          throws IOException {
                  String province 
          = req.getParameter("province");
                  StringBuffer cities 
          = new StringBuffer("<cities>");
                  
                  
          if("jiangsu".equals(province)){
                      cities.append(
          "<city>Nanjing</city>");
                      cities.append(
          "<city>Zhenjiang</city>");
                  }
          else if("zhejiang".equals(province)){
                      cities.append(
          "<city>Hanzhou</city>");
                      cities.append(
          "<city>Wenzhou</city>");
                  }        
                  
                  PrintWriter writer 
          = response.getWriter();    
                  cities.append(
          "</cities>");
                  response.setContentType(
          "text/xml");
                  writer.write(cities.toString());
                  writer.close();
              }
          }




          主站蜘蛛池模板: 永善县| 饶河县| 保德县| 宝兴县| 鄂托克旗| 临安市| 凭祥市| 静安区| 中方县| 玛多县| 清涧县| 碌曲县| 利川市| 榆社县| 新闻| 伊通| 峨边| 临西县| 特克斯县| 蒙城县| 翼城县| 镇坪县| 天全县| 翁源县| 绥芬河市| 新沂市| 沁源县| 尖扎县| 玉山县| 祁连县| 宁安市| 昂仁县| 邳州市| 东兴市| 商南县| 车险| 浦城县| 黔江区| 南郑县| 尉犁县| 宁化县|