设计思路
利用两个AJAX一个作为数据的处理一个作为进度条的处理当数据在处理的过程中根据处理数据的数值变化来获取进度条的进度。
jsp页面
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!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=GBK">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
var Timer;
$.ajax({
url:"ProServlet",
type:"post",
dataType:"text",
success:function(){
window.clearInterval(Timer);
alert("加载完成");
}
})
var Timer=setInterval("doPro()",1000);
});
});
function doPro(){
$.ajax({
url:"Pro",
type:"post",
dataType:"json",
success:function(rs){
$("#dd").css("width",""+((rs.success/rs.total)*100)+"%");
$("#dd").html((rs.success/rs.total)*100+"%");
}
})
}
</script>
</head>
<body>
<button id="btn">开始</button>
<div style="width: 200px; height: 20px; border:1px green solid; margin: auto; border-radius: 10px;">
<div id=dd style="width:0px; height: 20px; border-radius: 10px;"> </div>
</div>
</body>
</html>
进度条参数值(可随意定义)
package com.progress;
public class Progress {
private int total; //总数量
private int success;//完成的数量
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
}
两个selvet进行进度条数据的处理
package com.progress;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JFrame;
/**
* Servlet implementation class ProServlet
*/
@WebServlet("/ProServlet")
public class ProServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("gbk");
Progress pr=new Progress();
int j=10;
pr.setTotal(10);
for(int i=0;i<=j;i++){
try {
Thread.sleep(1000);
pr.setSuccess(i);
request.getSession().setAttribute("pr", pr);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(i);
}
}
}
//。。。
package com.progress;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Pro
*/
@WebServlet("/Pro")
public class Pro extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Pro() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Progress pr= (Progress)request.getSession().getAttribute("pr");
String json="{\"success\":\""+pr.getSuccess()+"\",\"total\":\""+pr.getTotal()+"\"}";
response.getWriter().print(json);
}
}
package com.progress;
public class Progress {
private int total; //总数量
private int success;//完成的数量
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
}