1. package mybean;  
  2.  
  3. public class TaskBean extends Thread{  
  4.  
  5. public TaskBean(){  
  6. }  
  7.  
  8. private int percent=0;  
  9. private boolean complete=false;  
  10.  
  11. public synchronized void run() {  
  12. while(true){  
  13. if(percent>=100){  
  14. complete=true;  
  15. return;  
  16. }  
  17. try {  
  18. Thread.sleep(1000);  
  19. } catch (Exception e) {  
  20. System.err.println("TaskBean.run()"+e.getMessage());  
  21. }  
  22. percent+=10;  
  23. }  
  24. }  
  25. public boolean isComplete(){  
  26. return complete;  
  27. }  
  28. public int getPercent(){  
  29. return percent;  
  30. }  
  31. }  
  32.  

start.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme() + "://" 
  5. + request.getServerName() + ":" + request.getServerPort()  
  6. + path + "/";  
  7. %>  
  8. <html>  
  9. <head>  
  10. <base href="<%=basePath%>">  
  11. <title>用javabean在JSP页面中实现进度条效果</title>  
  12. </head>  
  13.  
  14. <body>  
  15. <jsp:useBean id="progress" scope="session" class="mybean.TaskBean"></jsp:useBean>  
  16. <% new Thread(progress).start(); %>  
  17. <jsp:forward page="status.jsp"></jsp:forward>  
  18. </body>  
  19. </html> 

status.jsp
 

  1. <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9. <head>  
  10. <base href="<%=basePath%>">  
  11. <title>进度条</title>  
  12. </head>  
  13. <jsp:useBean id="progress" scope="session" class="mybean.TaskBean"></jsp:useBean>  
  14. <%   
  15. if(!progress.isComplete()){   
  16. %>  
  17. <script type="text/javascript">  
  18. <!--  
  19. function refresh(){  
  20. document.location="status.jsp";  
  21. }  
  22. setTimeout("refresh()",1000);  
  23. -->  
  24. </script>  
  25. <%} %>  
  26. <body>  
  27. <%   
  28. int percent=progress.getPercent();  
  29. %>  
  30. <table width="80%" cellspacing="0" cellpadding="0">  
  31. <div align="center"><%=percent %>%  
  32. <tr>  
  33. <%   
  34. for(int i=10;i<=percent;i+=10){  
  35. %>  
  36. <td width="10%" bgcolor="#000080">&nbsp;</td>  
  37. <%} %>  
  38. <%   
  39. for(int i=10;i<=100;i+=10){  
  40. %>  
  41. <td width="10%">&nbsp;</td>  
  42. <%} %>  
  43. </tr>  
  44. </div>  
  45. </table>  
  46. <%   
  47. if(progress.isComplete())  
  48. out.println("<br><font color='red'>任务完成</font>");  
  49. %>  
  50. </body>  
  51. </html>