您的位置:首页 > Web前端 > JavaScript

jsp+javascript进度条显示源码

2008-03-27 21:11 381 查看
游览器中打开status.jsp

文件start.jsp--------
<% session.removeAttribute("task");%>//为了使每次开始都新建一个bean对象
<jsp:useBean id = "task" scope = "session" class = "test.barBean.TaskBean"/>
<%task.setRunning(true); %>
<%new Thread(task).start();%>
<jsp:forward page = "status.jsp"/>

文件stop.jsp---------
<jsp:useBean id = "task" scope = "session" class = "test.barBean.TaskBean"/>
<%task.setRunning(false);%>
<jsp:forward page = "status.jsp"/>

文件status.jsp-----------
<%@ page language="java" contentType = "text/html;charset = gb2312" import = "java.sql.*" errorPage = ""%>
<jsp:useBean id = "task" scope = "session" class = "test.barBean.TaskBean"/>
<html>
<head>
<title>JSP进度条</title>
<%if(task.isRunning()){ %>
<script type="text/javascript">
//设置页面刷新的时间
setTimeout("location = 'status.jsp'",1000);
</script>
<%} %>
</head>

<body>
<h1 align="center">JSP进度条</h1>
<h2 align="center">
结果:<%= task.getResult() %><br>
<%int percent = task.getPercent(); %>
<%=percent %>
</h2>
<table width="60%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr>
<%for(int i = 10;i<=percent;i+=10){ %>
<td width="10%" bgcolor="#000080"> </td>
<%} %>
<%for(int i = 100;i>percent;i-=10){ %>
<td width="10%" > </td>
<%} %>
</tr>
</table>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<%if(task.isRunning()){ %>
正在执行
<%}else{ %>
<%if(task.isCompleted()) {%>
完成
<%}else if(!task.isStarted()){ %>
尚未开始
<%}else{ %>
已停止
<%} %>
<%} %>
</td>
</tr>

<tr>
<td align="center"><br>
<%if(task.isRunning()){ %>
<form action="stop.jsp" method="get">
<input type = submit value = 停止>
</form>
<%}else{ %>
<form action="start.jsp" method="get">
<input type = submit value = 开始>
</form>
<%} %>
</td>
</tr>
</table>
</body>
</html>

javabean文件 TaskBean------------------
package test.barBean;
import java.io.*;
public class TaskBean implements Runnable,Serializable{
private int counter;
private int sum;
private boolean started;
private boolean running;
private int sleep;
public TaskBean(){
counter = 0;
sum = 0;
started = false;
running = false;
sleep = 100;
}
protected void work(){
try{
Thread.sleep(sleep);
counter++;
sum += counter;
}catch(InterruptedException e){
setRunning(false);
}
}
public synchronized int getPercent() {
return counter;
}
public synchronized boolean isStarted() {
return started;
}
public boolean isRunning() {
return running;
}
public synchronized void setRunning(boolean running) {
this.running = running;
if(running)
started = true;
}

public synchronized Object getResult(){
if(isCompleted())
return new Integer(sum);
else
return null;
}
public boolean isCompleted() {
// TODO Auto-generated method stub
return counter ==100;
}
public void run() {
// TODO Auto-generated method stub
try{
setRunning(true);
while(isRunning()&&!isCompleted())
work();
}finally{
setRunning(false);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: