java web 服务器端执行的进度条

怎么做一个进度条上传附件进度后台执行的百分比什么时候执行完JSPJAVA代码都行... 怎么做一个进度条 上传附件进度 后台执行的百分比 什么时候执行完 JSP JAVA代码都行 展开
 我来答
匿名用户
2013-11-10
展开全部
开始页面: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()) { %>
正在执行
<% } 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;
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 synchronized boolean isCompleted() {
return counter == 100;
}
public synchronized 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 void run() {
try {
setRunning(true);
while (isRunning() && !isCompleted()) {
work();
}
}
finally {
setRunning(false);
}
}
}
匿名用户
2013-11-10
展开全部
这种效果网上很多的, 上gby 搜下.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式