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

          Ajax學習二

          Posted on 2009-11-15 09:28 笑看人生 閱讀(254) 評論(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();
              }
          }




          主站蜘蛛池模板: 米脂县| 平顺县| 开封县| 手机| 登封市| 贵德县| 翼城县| 凤凰县| 青田县| 武冈市| 临清市| 台北市| 区。| 噶尔县| 紫阳县| 兴化市| 安国市| 新平| 南澳县| 宣恩县| 富川| 呈贡县| 白水县| 绥棱县| 交口县| 那坡县| 大田县| 平南县| 靖江市| 东安县| 惠水县| 敖汉旗| 柘荣县| 惠安县| 济阳县| 宁德市| 亚东县| 康马县| 临沧市| 宝应县| 准格尔旗|