jsp进度条

分类: Jsp   出处:iocblog整理  更新时间:2008-06-20   添加到收藏  

开始页面:start.jsp

<%@ page contenttype="text/html; charset=gbk" %>
<% session.removeattribute("task"); %>

<jsp:usebean id="task" scope="session"  class="progress.taskbean"/>

<% task.setrunning(true); %>

<% new thread(task).start(); %>

<jsp:forward page="status.jsp"/>

状态页面:status.jsp

<%@ page contenttype="text/html; charset=gbk" %>
<jsp:usebean id="task" scope="session" class="progress.taskbean"/>
<html>
  <head>

  <title>jsp进度条</title>

  <% if (task.isrunning()) { %>

    <script type="" language="javascript">

      settimeout("location='status.jsp'", 1000);

    </script>

  <% } %>

</head>

<body bgcolor="">

<h1 align="center">jsp进度条</h1>

  <h2 align="center">

    结果: <%= task.getresult() %><br>

    <% int percent = task.getpercent(); %>

    <%= percent %>%

  </h2>

  <table width="60%" align="center"

       cellpadding=0 cellspacing=2>

    <tr>

      <% for (int i = 10; i <= percent; i += 10) { %>

        <td width="10%" height="10" bgcolor="red"> </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()) { %>

          正在执行[iocblog.net 来源]

        <% } else { %>

          <% if (task.iscompleted()) { %>

            完成

          <% } else if (!task.isstarted()) { %>

            尚未开始

          <% } else { %>

            已停止

          <% } %>

        <% } %>

      </td>

    </tr>
<tr>

      <td align="center">

        <br>

        <% if (task.isrunning()) { %>

          <form method="get" action="stop.jsp">

            <input type="submit" ="停止">

          </form>

        <% } else { %>

          <form method="get" action="start.jsp">

            <input type="submit" ="开始">

          </form>

        <% } %>

      </td>

    </tr>

  </table>

</body></html>

停止页面:stop.jsp

<%@ page contenttype="text/html; charset=gbk" %>
<jsp:usebean id="task" scope="session" class="progress.taskbean"/>

<% task.setrunning(false); %>

<jsp:forward page="status.jsp"/>

业务逻辑bean:taskbean.java

package progress;

import java.io.serializable;

/**
 * 首先我们设计一个taskbean类,它实现java.lang.runnable接口,
 * 其run()方法在一个由jsp页面(start.jsp)启动的独立线程中运行。
 * 终止run()方法执行由另一个jsp页面stop.jsp负责。
 * http://blog.knowsky.com/
 * taskbean类还实现了java.io.serializable接口,
 * 这样jsp页面就可以将它作为javabean调用
 * */
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;

  }
  /**
   * taskbean包含的“繁重任务”是计算1+2+3…+100的值,
   * 不过它不通过100*(100+1)/2=5050公式计算,而是由run()方法
   * 调用work()方法100次完成计算。work()方法的代码如下所示,
   * 其中调用thread.sleep()是为了确保任务总耗时约10秒。
   * */
  protected void work() {

    try {

      thread.sleep(sleep);

      counter++;

      sum += counter;

    }
    catch (interruptedexception e) {
      setrunning(false);

    }

  }
  //status.jsp页面通过调用下面的getpercent()方法获得任务的完成状况:
  public synchronized int getpercent() {

    return counter;

  }
  //如果任务已经启动,isstarted()方法将返回true:
  public synchronized boolean isstarted() {

    return started;

  }
  //如果任务已经完成,iscompleted()方法将返回true
  public synchronized boolean iscompleted() {

    return counter == 100;

  }
  //如果任务正在运行,isrunning()方法将返回true:
  public synchronized boolean isrunning() {

    return running;

  }
  /**
   * setrunning()方法由start.jsp或stop.jsp调用,
   * 当running参数是true时。setrunning()方法还要将任务标记为“已经启动”。
   * 调用setrunning(false)表示要求run()方法停止执行。[iocblog.net 来源]
   * */
  public synchronized void setrunning(boolean running) {

    this.running = running;

    if (running) {

      started = true;
    }

  }
  //任务执行完毕后,调用getresult()方法返回计算结果;如果任务尚未执行完毕,它返回null:
  public synchronized object getresult() {

    if (iscompleted()) {

      return new integer(sum);
    }

    else {

      return null;
    }

  }
  /**
   * 当running标记为true、completed标记为false时,
   * run()方法调用work()。在实际应用中,run()方法也许要
   * 执行复杂的sql查询、解析大型xml文档,或者调用消耗大量
   * cpu时间的ejb方法。注意“繁重的任务”可能要在远程服务器
   * 上执行。报告结果的jsp页面有两种选择:或者等待任务结束,或者使用一个进度条。
   * */
  public void run() {

    try {

      setrunning(true);

      while (isrunning() && !iscompleted()) {

        work();
      }

    }
    finally {

      setrunning(false);

    }

  }

}


Tag: 进度条



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。