用javabean在JSP页面中实现进度条效果


public class TaskBean extends Thread{

public TaskBean(){
}

private int percent=0;
private boolean complete=false;

public synchronized void run() {
while(true){
if(percent>=100){
complete=true;
return;
}
try {
Thread.sleep(1000);
} catch (Exception e) {
System.err.println("TaskBean.run()"+e.getMessage());
}
percent+=10;
}
}
public boolean isComplete(){
return complete;
}
public int getPercent(){
return percent;
}
}


start.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>用javabean在JSP页面中实现进度条效果</title>
</head>

<body>
<jsp:useBean id="progress" scope="session" class="mybean.TaskBean"></jsp:useBean>
<% new Thread(progress).start(); %>
<jsp:forward page="status.jsp"></jsp:forward>
</body>
</html>

status.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>进度条</title>
</head>
<jsp:useBean id="progress" scope="session" class="mybean.TaskBean"></jsp:useBean>
<%
if(!progress.isComplete()){
%>
<script type="text/javascript">
<!--
function refresh(){
document.location="status.jsp";
}
setTimeout("refresh()",1000);
-->
</script>
<%} %>
<body>
<%
int percent=progress.getPercent();
%>
<table width="80%" cellspacing="0" cellpadding="0">
<div align="center"><%=percent %>%
<tr>
<%
for(int i=10;i<=percent;i+=10){
%>
<td width="10%" bgcolor="#000080"> </td>
<%} %>
<%
for(int i=10;i<=100;i+=10){
%>
<td width="10%"> </td>
<%} %>
</tr>
</div>
</table>
<%
if(progress.isComplete())
out.println("<br><font color='red'>任务完成</font>");
%>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值