java web 服务器端执行的进度条
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);
}
}
}
<%@ 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 搜下.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询