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);
}
}
}
文件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);
}
}
}
相关文章推荐
- jsp+javascript进度条显示源码
- Javascript进度条显示
- javascript 实现 web页面显示百分比的进度条
- JSP实现留言板的JavaBean类和分页显示的源码
- [转]以列表形式显示动态加载的数据(JavaScript,Java,JSP,HTML)
- javascript全屏显示源码
- Javascript进度条显示javascript
- JSP中文乱码显示及javascript与JSP变量传递问题
- javascript jsp页面动态显示系统时间 测试可用 多浏览器通过测试 (2)
- jsp和javascript显示系统时间
- struts <html:select> 加入jsp后javascript 不能正常运行,firebug显示No Javascript on this page
- (JSP)在文本域中显示超链接new——JavaScript部分
- Eclipse color theme jsp javascript显示问题
- jsp中 根据传入值,采用javascript自动显示标签下拉选项
- 一个简单的用javascript实现的页面内容过滤显示小程序源码
- libcurl+ncurses 分段range批量下载和进度条显示源码实例
- JavaScript进度条设计源码实例
- php+javascript动态显示服务器运行程序的进度条
- JavaScript进度条设计源码实例
- winform 加载窗体时弹出另一个窗体并显示进度条的源码