Ajax Implementation Progress bar

Source: Internet
Author: User
Tags object key visibility window client

An AJAX implementation of the progress bar, the following code:

 
 
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
  2. <title>ajax Progress bar-www.cxybl.com</title>
  3. <script type= "Text/javascript" >
  4. var xmlHttp;
  5. var key;
  6. var bar_color = ' gray ';//The Color of the progress bar
  7. var span_id = "Block";
  8. var clear = "";
  9. function createxmlhttprequest ()//Create XMLHttpRequest Object
  10. {
  11. if (window. ActiveXObject)
  12. {
  13. XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
  14. }
  15. else if (window. XMLHttpRequest)
  16. {
  17. XmlHttp = new XMLHttpRequest ();
  18. }
  19. }
  20. function Go ()
  21. {
  22. Createxmlhttprequest ()///Create XMLHttpRequest Object
  23. Checkdiv ()//Show scroll bar
  24. Xmlhttp.onreadystatechange = callback;//Set callback function
  25. var url = "/ajaxdemo/servlet/progressbarservlet?task=create";//Requested Address
  26. var button = document.getElementById ("Go");
  27. button.disabled = true;//Settings button not available
  28. Xmlhttp.open ("Get", url,true);//Open connection to server
  29. Xmlhttp.send ()//Send Request
  30. }
  31. function CallBack ()
  32. {
  33. if (xmlhttp.readystate = 4)
  34. {
  35. if (Xmlhttp.status = 200)
  36. {
  37. SetTimeout ("Pollserver ()", 500);//Timed Call
  38. }
  39. }
  40. }
  41. function Pollserver ()
  42. {
  43. Createxmlhttprequest ();
  44. var url= "/ajaxdemo/servlet/progressbarservlet?task=poll&key=" +key;
  45. Xmlhttp.onreadystatechange = Pollcallback;
  46. Xmlhttp.open ("Get", url,true);
  47. Xmlhttp.send ();
  48. }
  49. function Pollcallback ()
  50. {
  51. if (xmlhttp.readystate = 4)
  52. {
  53. if (Xmlhttp.status = 200)
  54. {
  55. var percent_complete = XmlHttp.responseXML.getElementsByTagName ("percent") [0]
  56. . firstchild.data;//get response information from the server side
  57. var index = Processresult (percent_complete);
  58. for (var i = 1; i<=index; i++)
  59. {
  60. var elem = document.getElementById ("block" +i);
  61. elem.innerhtml = clear;
  62. Elem.style.backgroundColor = Bar_color;
  63. var Next_cell = i+1;
  64. if (Next_cell > Index && next_cell <= 9)
  65. {
  66. document.getElementById ("block" +next_cell). InnerHTML = percent_complete + "%";
  67. }
  68. }
  69. if (index <9)
  70. {
  71. SetTimeout ("Pollserver ()", 500);
  72. }
  73. Else
  74. {
  75. document.getElementById ("complete"). InnerHTML = "complete!";
  76. document.getElementById ("Go"). Disabled = false;
  77. }
  78. }
  79. }
  80. }
  81. function Processresult (percent_complete)
  82. {
  83. var ind;
  84. if (percent_complete.length = 1)
  85. {
  86. IND = 1;
  87. }
  88. else if (percent_complete.length = 2)
  89. {
  90. IND = percent_complete.substring (0,1);
  91. }
  92. Else
  93. {
  94. IND = 9;
  95. }
  96. return IND;
  97. }
  98. function Checkdiv ()
  99. {
  100. var Progress_bar = document.getElementById ("ProgressBar");
  101. if (progress_bar.style.visibility = = "visible")
  102. {
  103. Clearbar ();
  104. document.getElementById ("complete"). InnerHTML = "";
  105. }
  106. Else
  107. {
  108. progress_bar.style.visibility = "visible";
  109. }
  110. }
  111. function Clearbar ()
  112. {
  113. for (var i =1; i<10; i++)
  114. {
  115. var elem = document.getElementById ("block" +i);
  116. elem.innerhtml = clear;
  117. Elem.style.backgroundColor = "White";
  118. }
  119. }
  120. </script>
  121. <body>
  122. Launch long-running Process:
  123. <input type= ' button ' value= ' Launch ' id= ' go ' onclick= ' Go () '/>
  124. <p>
  125. <table align= "center" >
  126. <tbody>
  127. <tr>
  128. <td>
  129. <div id= "ProgressBar" style= "Padding:2px;border:solid black 2px;visibility:hidden" >
  130. <span id= "Block1" > </span>
  131. <span id= "Block2" > </span>
  132. <span id= "Block3" > </span>
  133. <span id= "Block4" > </span>
  134. <span id= "Block5" > </span>
  135. <span id= "Block6" > </span>
  136. <span id= "Block7" > </span>
  137. <span id= "Block8" > </span>
  138. <span id= "Block9" > </span>
  139. </div>
  140. </td>
  141. </tr>
  142. <TR><TD align= "center" id= "complete" ></td></tr>
  143. </tbody>
  144. </table>
  145. </body>
  146. Package CN. Ajax.test;
  147. Import java.io.IOException;
  148. Import Java.io.PrintWriter;
  149. Import javax.servlet.ServletException;
  150. Import Javax.servlet.http.HttpServlet;
  151. Import Javax.servlet.http.HttpServletRequest;
  152. Import Javax.servlet.http.HttpServletResponse;
  153. @SuppressWarnings ("Serial")
  154. public class Progressbarservlet extends HttpServlet {
  155. private int counter = 1;
  156. /**
  157. * The Doget method of the servlet. <br>
  158. *
  159. * This is called when a form has it tag value method equals to get.
  160. *
  161. * @param request the request send by the client to the server
  162. * @param response The response send by the server to the client
  163. * @throws servletexception If an error occurred
  164. * @throws IOException If an error occurred
  165. */
  166. public void doget (HttpServletRequest request, httpservletresponse response)
  167. Throws Servletexception, IOException {
  168. String task = Request.getparameter ("task");
  169. String res= "";
  170. if (Task.equals ("create")) {
  171. res = "<key>1</key>";
  172. counter = 1;
  173. }
  174. else{
  175. String percent = "";
  176. Switch (counter) {
  177. Case 1:percent = "ten";
  178. Case 2:percent = "n";
  179. Case 3:percent = "a";
  180. Case 4:percent = "Wuyi";
  181. Case 5:percent = "the";
  182. Case 6:percent = "by";
  183. Case 7:percent = "the";
  184. Case 8:percent = "a";
  185. }
  186. counter++;
  187. res = "<percent>" +percent+ "</percent>";
  188. }
  189. PrintWriter out = Response.getwriter ();
  190. Response.setcontenttype ("Text/xml");
  191. Response.setheader ("Cache-control", "No-cache");
  192. Out.println ("<response>");
  193. Out.println (RES);
  194. Out.println ("</response>");
  195. Out.close ();
  196. }
  197. /**
  198. * The DoPost method of the servlet. <br>
  199. *
  200. * This is called when a form, has its tag value, equals to post.
  201. *
  202. * @param request the request send by the client to the server
  203. * @param response The response send by the server to the client
  204. * @throws servletexception If an error occurred
  205. * @throws IOException If an error occurred
  206. */
  207. public void DoPost (HttpServletRequest request, httpservletresponse response)
  208. Throws Servletexception, IOException {
  209. Doget (request, response);
  210. }
  211. }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.