ajax结合js实现服务器

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#divProgress{width:300px;height:24px;position:relative;}
#divProgress div{position:absolute;left:0;top:0;height:24px;}
#progressBg{background-color:#B9F8F9;z-index:10;}
#progresstext{z-index:15;text-align:center;width:100%;}
</style>
</head>
<body>
<div id="divProgress">
<div id="progressBg"></div>
<div id="progresstext"></div>
</div>
<br />
<button onclick="send()">�ύ���</button>
<script>
var t = document.getElementById("progresstext");
var bg = document.getElementById("progressBg");
function send(){
t.innerHTML = "loading...";
bg.style.width = "0px";
var xhr = new window.XMLHttpRequest();
if(!window.XMLHttpRequest){
try {
xhr = new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
xhr.open("post","index.jsp?count=6");
var oldSize=0;
xhr.onreadystatechange = function(){
if(xhr.readyState > 2){
var tmpText = xhr.responseText.substring(oldSize);
oldSize = xhr.responseText.length;
if(tmpText.length > 0 ){
t.innerHTML = tmpText + "/6";
var width = parseInt(tmpText)/6*300;
bg.style.width = width+"px";
}
}
if(xhr.readyState == 4){

t.innerHTML = "ִ�����";
bg.style.width = "300px";
}
}
xhr.send(null);
}
</script>
</body>
</html>

服务端:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% // 下面设置Content-Type:application/x-javascript 是为了适应Webkit的浏览器(chrome,safari) response.setHeader("Content-Type","application/x-javascript"); int count = 6; // 处理6条数据 for(int i=0;i<count;i++){ // 处理完毕一条,输出结果到客户端 out.println(i+1); out.flush(); // 这里假设每条数据处理时间为1秒 Thread.currentThread().sleep(1000); } %>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...