jsp+ajax自動刷新局部頁面


  通過 AJAX,JavaScript 可使用 JavaScript 的 XMLHttpRequest 對象來直接與服務器進行通信。通過這個對象, JavaScript 可在不重載頁面的情況與 Web 服務器交換數據。

     AJAX 在瀏覽器與 Web 服務器之間使用異步數據傳輸(HTTP 請求),這樣就可使網頁從服務器請求少量的信息,而不是整個頁面。

     實驗中利用JSP+ajax來實現自動刷新頁面,並讀/寫數據庫中的數據。

    下面介紹一下利用JSP+ajax來實現局部頁面刷新的小例子:

處理ajax請求的jsp文件:ajax.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();  
    %>   

 

發送ajax請求的jsp文件:zx.jsp

    <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 = "ajax.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 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>   

  效果如下(頁面上的值自動變化):

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM