解讀jsp+ajax自動刷新實例_AJAX教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:AJAX教程之由省份選擇城市
      一、test.html html head titleMyHtml.html/title meta http-equiv=keywords content=keyword1,keyword2,keyword3 meta http-equiv=description content=this is my page /head script type=text/javascript function getResult(stateVal) { var u

      一、JSP文件:auto.jsp
      <%@ page contentType="text/html; charset=gb2312" %>

      <%
      //設置輸出信息的格式及字符集
      response.setContentType("text/xml; charset=UTF-8");
      response.setHeader("Cache-Control","no-cache");
      out.println("<response>");

      for(int i=0;i<2;i++){
      out.println("<name>"+(int)(Math.random()*10)+"型筆記本</name>");
      out.println("<count>" +(int)(Math.random()*100)+ "</count>");
      }
      out.println("</response>");
      out.close();

      %>

      二、html文件:autoRefresh.html
      <head>
      <META http-equiv=Content-Type content="text/html; charset=gb2312">
      </head>
      <script language="javascript">
      var XMLHttpReq;
      //創建XMLHttpRequest對象
      function createXMLHttpRequest() {
      if(window.XMLHttpRequest) { //Mozilla 瀏覽器
      XMLHttpReq = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) { // IE瀏覽器
      try {
      XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
      XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
      }
      }
      }
      //發送請求函數
      function sendRequest() {
      createXMLHttpRequest();
      var url = "auto.jsp";
      XMLHttpReq.open("GET", url, true);
      XMLHttpReq.onreadystatechange = processResponse;//指定響應函數
      XMLHttpReq.send(null); // 發送請求
      }
      // 處理返回信息函數
      function processResponse() {
      if (XMLHttpReq.readyState == 4) { // 判斷對象狀態
      if (XMLHttpReq.status == 200) { // 信息已經成功返回,開始處理信息
      DisplayHot();
      setTimeout("sendRequest()", 1000);
      } else { //頁面不正常
      window.alert("您所請求的頁面有異常。");
      }
      }
      }
      function DisplayHot() {
      var name = XMLHttpReq.responseXML.getElementsByTagName("name")[0].firstChild.nodeValue;
      var count = XMLHttpReq.responseXML.getElementsByTagName("count")[0].firstChild.nodeValue;
      document.getElementById("product").innerHTML = name;
      document.getElementById("count").innerHTML = count;
      }

      </script>

      <body onload =sendRequest()>
      <table style="BORDER-COLLAPSE: collapse" borderColor=#111111
      cellSpacing=0 cellPadding=0 width=200 bgColor=#f5efe7 border=0>
      <TR>
      <TD align=middle height=4 colspan="2"><IMG height=4
      src="images/promo_list_top.gif" width="100%"
      border=0>
      </TD>
      </TR>
      <TR>
      <TD align=middle bgColor=#dbc2b0
      height=19 colspan="2"><B>正在熱賣的筆記本</B>
      </TD>
      </TR>
      <tr>
      <td height="20">
      型號:
      </td>
      <td height="20" id="product">
      </td>
      </tr>
      <tr>
      <td height="20">
      銷售數量:
      </td>
      <td height="20" id="count">
      </td>
      </tr>
      </body>
      </table>

       

      分享:xmlHttpRequest實踐之無刷新驗證用戶名
      現在好多網站上的注冊都用了無刷新驗證用戶名,這種效果咋看感覺很復雜很難實現,其實它里面用到了Ajax中的核心xmlHttpRequest這個類,如果只是單單想實現這個效果,壓根就不用引用Ajax.Net中的組件,因為感覺有點大材小用,下面是具體實現這種效果的方法,希望能給

      來源:模板無憂//所屬分類:AJAX教程/更新時間:2010-03-05
      相關AJAX教程