时间:2021-07-01 10:21:17 帮助过:6人阅读
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <%! int counter = 1;//注意:多用户将共享此变量,此进度条只适合单用户 %> <% String task = request.getParameter("task"); String res = ""; if (task.equals("create")) { res = "1"; counter = 1; } else { String percent = ""; switch (counter) { case 1: percent = "10"; break; case 2: percent = "23"; break; case 3: percent = "35"; break; case 4: percent = "51"; break; case 5: percent = "64"; break; case 6: percent = "73"; break; case 7: percent = "89"; break; case 8: percent = "100"; break; } counter++; res = "" + percent + " "; } // PrintWriter out = response.getWriter(); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); out.println(""); out.println(res); out.println(" "); out.close(); %>
将上述代码保存,名称为JspprogressBar.html。在该文件中,JavaScript函数createXMLHttpRequest()主要用来创建XMLHttpRequest对象,go()函数完成想服务器端发送异步请求,该函数在网页加载时被调用,其主要作用就是通知服务器,并在客户端开始运行进度条。GoCallback()函数主要用于处理服务端的响应,并每隔2秒调用pollServer()函数也是用于向服务端发送异步请求,主要请求服务器端响应的百分数字。PollCallback()函数主要用于处理服务器端响应,即依据服务器端的返回的数字,指定进度条的显示状态。这里需要注意的是,goCallback()函数只执行一次,而PollCallback()函数可以执行多次,其余的三个函数都是实现进度条的辅助函数。
3,运行
将上述两个Jsp文件复制到JSP目录下。运行效果图如下: