Ajax创建自动刷新页面

前端之家收集整理的这篇文章主要介绍了Ajax创建自动刷新页面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.dynamicUpdate.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- 自动刷新页面 -->
<html>
  <head>
    <title>Ajax Dynamic Update</title>
    <script type="text/javascript">
        var xmlHttp;

        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();                
            }
        }
        
        function doStart() {
            createXMLHttpRequest();
            var url = "DynamicUpdateServlet?task=reset";
            xmlHttp.open("GET",url,true);
            xmlHttp.onreadystatechange = startCallback;
            xmlHttp.send(null);
        }

        function startCallback() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {                    
                    setTimeout("pollServer()",5000);
                    refreshTime();
                }
            }
        }
        
        function pollServer() {
            createXMLHttpRequest();
            var url = "DynamicUpdateServlet?task=foo";
            xmlHttp.open("GET",true);
            xmlHttp.onreadystatechange = pollCallback;
            xmlHttp.send(null);
        }
        
        function refreshTime(){
            var time_span = document.getElementById("time");
            var time_val = time_span.innerHTML;

            var int_val = parseInt(time_val);
            var new_int_val = int_val - 1;
            
            if (new_int_val > -1) {
                setTimeout("refreshTime()",1000);
                time_span.innerHTML = new_int_val;                
            } else {
                time_span.innerHTML = 5;
            }
        }
        
        function pollCallback() {
            if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {                
                    var message = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;                  
                    
                    if (message != "done") {
                        var new_row = createRow(message);
                        var table = document.getElementById("dynamicUpdateArea");
                        var table_body = table.getElementsByTagName("tbody").item(0);
                        var first_row = table_body.getElementsByTagName("tr").item(1);
                        table_body.insertBefore(new_row,first_row);                        
                        setTimeout("pollServer()",5000);
                        refreshTime();
                    }
                }
            }
        }
        
        function createRow(message) {
            var row = document.createElement("tr");
            var cell = document.createElement("td");
            var cell_data = document.createTextNode(message);
            cell.appendChild(cell_data);
            row.appendChild(cell);
            return row;
        }
    </script>
  </head>
  <body>
    <h1>Ajax Dynamic Update Example</h1>
    This page will automatically update itself: 
        <input type="button" value="Launch" id="go" onclick="doStart();"/>
    <p>
    Page will refresh in <span id="time">5</span> seconds.
    <p>
    <table id="dynamicUpdateArea" align="left">
        <tbody>
            <tr id="row0"><td></td></tr>
        </tbody>
    </table>
  </body>
</html>

2.DynamicUpdateServlet.java

/*
 * DynamicUpdateServlet.java
 *
 * Created on June 26,2005,2:39 PM
 */

package com.wch.ajax.servlet;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author nate
 * @version
 */
public class DynamicUpdateServlet extends HttpServlet {
    private int counter = 1;
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException,IOException {
        String res = "";
        String task = request.getParameter("task");
        String message = "";        
        
        if (task.equals("reset")) {
            counter = 1;
        } else {
            switch (counter) {
                case 1: message = "Steve walks on stage"; break;
                case 2: message = "iPods rock"; break;
                case 3: message = "Steve says Macs rule"; break;
                case 4: message = "Change is coming"; break;
                case 5: message = "Yes,OS X runs on Intel - has for years"; break;
                case 6: message = "Macs will soon have Intel chips"; break;
                case 7: message = "done"; break;
            }
            counter++;
        }

        res = "<message>" + message + "</message>";
        
        PrintWriter out = response.getWriter();
        response.setContentType("text/xml");
        response.setHeader("Cache-Control","no-cache");
        out.println("<response>");
        out.println(res);
        out.println("</response>");
        out.close();
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request,IOException {
        doGet(request,response);
    }
    

    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
}

3.web.xml

<servlet>
    <servlet-name>DynamicUpdateServlet</servlet-name>
    <servlet-class>com.wch.ajax.servlet.DynamicUpdateServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DynamicUpdateServlet</servlet-name>
    <url-pattern>/DynamicUpdateServlet</url-pattern>
  </servlet-mapping>
原文链接:https://www.f2er.com/ajax/166611.html

猜你在找的Ajax相关文章