游览器中打开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>
<%}