采用线程不断向页面传达递信息:实现如下:
import
java.io.Serializable;
public class TaskBean implements Runnable, Serializable {
private boolean started;
private boolean running;
private int sleep;
private Task task;
public TaskBean() {
started = false;
running = false;
sleep = 100;
task=new Task();
}
protected void work() {
try {
Thread.sleep(sleep);
task.work();
} catch (InterruptedException e) {
setRunning(false);
}
}
// 如果任务已经启动,isStarted()方法将返回true:
public synchronized boolean isStarted() {
return started;
}
// 如果任务已经完成,isCompleted()方法将返回true:
public synchronized boolean isCompleted() {
return task.isIfCompleted();
}
// 如果任务正在运行,isRunning()方法将返回true:
public synchronized boolean isRunning() {
return running;
}
public synchronized void setRunning(boolean running) {
this.running = running;
if (running)
started = true;
}
public void run() {
try {
setRunning(true);
while (isRunning() && !isCompleted())
work();
} finally {
setRunning(false);
}
}
}
public class TaskBean implements Runnable, Serializable {
private boolean started;
private boolean running;
private int sleep;
private Task task;
public TaskBean() {
started = false;
running = false;
sleep = 100;
task=new Task();
}
protected void work() {
try {
Thread.sleep(sleep);
task.work();
} catch (InterruptedException e) {
setRunning(false);
}
}
// 如果任务已经启动,isStarted()方法将返回true:
public synchronized boolean isStarted() {
return started;
}
// 如果任务已经完成,isCompleted()方法将返回true:
public synchronized boolean isCompleted() {
return task.isIfCompleted();
}
// 如果任务正在运行,isRunning()方法将返回true:
public synchronized boolean isRunning() {
return running;
}
public synchronized void setRunning(boolean running) {
this.running = running;
if (running)
started = true;
}
public void run() {
try {
setRunning(true);
while (isRunning() && !isCompleted())
work();
} finally {
setRunning(false);
}
}
}
package
com;
public class Task {
private boolean ifCompleted=false;
public Task() {
super();
// TODO Auto-generated constructor stub
}
public boolean isIfCompleted() {
return ifCompleted;
}
public void setIfCompleted(boolean ifCompleted) {
this.ifCompleted = ifCompleted;
}
public void work(){
for(int i=0;i<1000;i++){
for(int j=0;j<1000;j++)
for(int k=0;k<1000;k++)
for(int m=0;m<1000;m++)
for(int n=0;n<1000;n++)
for(int p=0;p<1000;p++)
System.out.println("aaa-->"+j);
}
this.setIfCompleted(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
在程序中启动:
TaskBean task=new TaskBean();
task.setRunning(true);
new Thread(task).start();
request.getSession.setAttribute("task");
或在页面中:
<%
session.removeAttribute("task");
%>
<jsp:useBean id="task" scope="session"
class="com.TaskBean"/>
<% task.setRunning(true); %>
<% new Thread(task).start(); %>
<jsp:forward page="status.jsp"/>
status.jsp:
<%@ page language="java" import="java.util.*,com.TaskBean" pageEncoding="GBK"%>
<jsp:useBean id="task" scope="session"
class="com.TaskBean"/>
//备注: <jsp:usebean>用于定位或示例一个javabeans组件。<jsp:usebean>首先会试图定位一个bean实例,如果这个bean不存在,那么<jsp:usebean>就会从一个class或模版中进行示例。
//
<HTML>
<HEAD>
<TITLE>JSP进度条</TITLE>
<% if (task.isRunning()) { %>
<SCRIPT LANGUAGE="JavaScript">
setTimeout("location='status.jsp'", 1000);
</SCRIPT>
<% } %>
</HEAD>
<BODY>
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD ALIGN="CENTER">
<% if (task.isRunning()) { %>
程序正在执行,请等待!
<% } else { %>
<% if (task.isCompleted()) { %>
数据生成完成!
<% } }%>
</TD>
</TR>
<TR>
<TD ALIGN="CENTER">
</TD>
</TR>
</TABLE>
</BODY></HTML>
public class Task {
private boolean ifCompleted=false;
public Task() {
super();
// TODO Auto-generated constructor stub
}
public boolean isIfCompleted() {
return ifCompleted;
}
public void setIfCompleted(boolean ifCompleted) {
this.ifCompleted = ifCompleted;
}
public void work(){
for(int i=0;i<1000;i++){
for(int j=0;j<1000;j++)
for(int k=0;k<1000;k++)
for(int m=0;m<1000;m++)
for(int n=0;n<1000;n++)
for(int p=0;p<1000;p++)
System.out.println("aaa-->"+j);
}
this.setIfCompleted(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
在程序中启动:
TaskBean task=new TaskBean();
task.setRunning(true);
new Thread(task).start();
request.getSession.setAttribute("task");
或在页面中:
<%
session.removeAttribute("task");
%>
<jsp:useBean id="task" scope="session"
class="com.TaskBean"/>
<% task.setRunning(true); %>
<% new Thread(task).start(); %>
<jsp:forward page="status.jsp"/>
status.jsp:
<%@ page language="java" import="java.util.*,com.TaskBean" pageEncoding="GBK"%>
<jsp:useBean id="task" scope="session"
class="com.TaskBean"/>
//备注: <jsp:usebean>用于定位或示例一个javabeans组件。<jsp:usebean>首先会试图定位一个bean实例,如果这个bean不存在,那么<jsp:usebean>就会从一个class或模版中进行示例。
//
<HTML>
<HEAD>
<TITLE>JSP进度条</TITLE>
<% if (task.isRunning()) { %>
<SCRIPT LANGUAGE="JavaScript">
setTimeout("location='status.jsp'", 1000);
</SCRIPT>
<% } %>
</HEAD>
<BODY>
<TABLE WIDTH="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0>
<TR>
<TD ALIGN="CENTER">
<% if (task.isRunning()) { %>
程序正在执行,请等待!
<% } else { %>
<% if (task.isCompleted()) { %>
数据生成完成!
<% } }%>
</TD>
</TR>
<TR>
<TD ALIGN="CENTER">
</TD>
</TR>
</TABLE>
</BODY></HTML>