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

          Ajax學習二

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




          主站蜘蛛池模板: 马公市| 徐汇区| 喜德县| 秭归县| 许昌市| 石楼县| 南丰县| 化州市| 梁平县| 德庆县| 宜都市| 莆田市| 大兴区| 甘孜县| 沅江市| 抚顺县| 平罗县| 广丰县| 子长县| 阿尔山市| 沙雅县| 沂源县| 辛集市| 清河县| 漯河市| 蒙自县| 望奎县| 福鼎市| 剑河县| 庆阳市| 宝坻区| 孙吴县| 望奎县| 安平县| 桐乡市| 沿河| 子长县| 饶平县| 龙江县| 根河市| 吴堡县|