`

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

    博客分类:
  • 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">&nbsp;</td>
      <%} %>
      <%  
       for(int i=10;i<=100;i+=10){
      %>
      <td width="10%">&nbsp;</td>
      <%} %>
      </tr>
      </div>
      </table>
      <% 
       if(progress.isComplete())
       out.println("<br><font color='red'>任务完成</font>");
      %>
    </body>
</html>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics