ajax进度条

1.进度条由DIV框和数个有背景颜色的SPAN组成;
2.初始化时DIV不可见,SPAN背景色与网页背景色一致;
3.根据服务器返回的进度百分比,计算出应显示几个SPAN块,并为SPAN块设置背景色;



progressBar.html:

程序代码

<!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=UTF-8">
    <title>ProgressBar</title>
    <script type="text/javascript" src="js/progressBar.js"></script>
</head>
<body>
    <h1>Ajax Progress Bar</h1>
    <input id="btn" type="button" value="Start" onClick="doStart();" />
    <br /><br />
    <div id="bar" style="width:156px; border:#000000 2px solid; padding:1px; visibility:hidden;">
        <span id="bar1" style="background-color:#FFFFFF">   </span>
        <span id="bar2" style="background-color:#FFFFFF">   </span>
        <span id="bar3" style="background-color:#FFFFFF">   </span>
        <span id="bar4" style="background-color:#FFFFFF">   </span>
        <span id="bar5" style="background-color:#FFFFFF">   </span>
        <span id="bar6" style="background-color:#FFFFFF">   </span>
        <span id="bar7" style="background-color:#FFFFFF">   </span>
        <span id="bar8" style="background-color:#FFFFFF">   </span>
        <span id="bar9" style="background-color:#FFFFFF">   </span>
        <span id="bar10" style="background-color:#FFFFFF">   </span>
        
    </div>
    <br />
    <div id="result"></div>
</body>
</html>



progressBar.js:

程序代码

var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function doStart() {
    init();
    document.getElementById("bar").style.visibility = "visible";
}

function init() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleChange;
    var url = "progressBar.mgc?action=init×tamp=" + new Date().getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function handleChange() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            refresh();
        }
    }
}

function refresh() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = callback;
    var url = "progressBar.mgc?action=run×tamp=" + new Date().getTime();
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    setTimeout("refresh()", "500");
}

function callback() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            parseResult();
        }
    }
}

function parseResult() {
    var xmlDoc = xmlHttp.responseXML;
    var pros = xmlDoc.getElementsByTagName("progress");
    var result = "";
    for (var i = 0; i < pros.length; i++) {
        var massage = pros[i].childNodes[0].childNodes[0].nodeValue;
        var percent = pros[i].childNodes[1].childNodes[0].nodeValue;
        result += massage + "[" + percent + "%]" + "<br />";
    }
    document.getElementById("result").innerHTML = result;
    clearBar();
    var percents = xmlDoc.getElementsByTagName("percent");
    var nowPercent = percents[percents.length - 1].childNodes[0].nodeValue;
    var count = parseInt(nowPercent / 10);
    for (var i = 0; i <= count; i++) {
        if (i != 0) {
            var bar = document.getElementById("bar" + i);
            bar.style.backgroundColor = "#bababa";
        }
    }
}

function clearBar() {
    for (var i = 1; i <= 10; i++) {
        var bar = document.getElementById("bar" + i);
        bar.style.backgroundColor = "#FFFFFF";
    }
}



progressBar.java:

程序代码

package cn.edu.ahau.mgc.ajax.progressbar;

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 ProgressBar extends HttpServlet {
    
    private int count;
    private StringBuffer str;
    public static final int TOTAL = 25;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("init".equals(action)) {
            this.count = 1;
            this.str = new StringBuffer();
        } else if ("run".equals(action)) {
            if (this.count <= TOTAL) {
                int percent = count * 100 / TOTAL;
                str.append("<progress><massage> 完成了 " + (this.count++) + "个任务</massage><percent>" + percent + "</percent></progress>");
            }
            response.setContentType("text/xml; charset=UTF-8");
            response.setHeader("Cache-Control", "no-cache");
            PrintWriter out = response.getWriter();
            out.print("<response>" + str + "</response>");
            out.flush();
            out.close();
        }
    }

}



web.xml:

程序代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ProgressBar</servlet-name>
    <servlet-class>cn.edu.ahau.mgc.ajax.progressbar.ProgressBar</servlet-class>
  </servlet>







  <servlet-mapping>
    <servlet-name>ProgressBar</servlet-name>
    <url-pattern>/progressBar.mgc</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

你可能感兴趣的:(Ajax,null,action,div,button,encoding)