jsp+javascript进度条显示源码

游览器中打开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">&nbsp;</td>
            <%} %>
            <%for(int i = 100;i>percent;i-=10){ %>
                <td width="10%" >&nbsp;</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);
        }
    }
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值