Ajax regularly updated automatically updated with the progress of the

Related Tags:

  In a recent project, a 3.5 features: cvs upload a file, then write the file and analytical database as often require an enormous paper, customers often need to complete this function 40 minutes, in the process, Pages do not have any tips, the user experience is very good? 
  Why not make a progress of the ajax? 
  This needs to complete the two-step: 
  1: write a simple ajax and realizing the simple function of the progress. 
  Second: the progress of this transformation can be used for the progress of the project. 

  A: The most simple of progress 
  1.    Client every two seconds createXMLHttpRequest sent a request to the server. Returned to the terminal to services and the progress of data. According to the service-return data, a table using javascript update the width, 
  This simulates a progress bar. 
  ProgressBar.html. Reads as follows: 

  <! DOCTYPE html PUBLIC "- / / w3c / / DTD HTML 4.01 Transitional / / EN"> 

<html>
<head>
  <title> Ajax Progress Bar </ title> 
  <script Type="text/javascript"> 
  Var xmlHttp; 
  Var key; 
  Function createXMLHttpRequest () ( 
  If (window.ActiveXObject) ( 
  XmlHttp = new ActiveXObject ( "Microsoft.XMLHTTP"); 
  ) 
  Else if (window.XMLHttpRequest) ( 
  XmlHttp = new XMLHttpRequest (); 
  ) 
  ) 

  Function go () ( 
  CreateXMLHttpRequest (); 
  ClearBar (); 
  Var url = "ProgressBarServlet? Create task ="; 
  Var button = document.getElementById ( "go"); 
  Button.disabled = true; 
  XmlHttp.open ( "GET", url, true); 
  XmlHttp.onreadystatechange = goCallback; 
  XmlHttp.send (null); 
  ) 

  Function goCallback () ( 
  If (xmlHttp.readyState == 4) ( 
  If (xmlHttp.status == 200) ( 
  SetTimeout ( "pollServer ()", 2000); 
  ) 
  ) 
  ) 

  Function pollServer () ( 
  CreateXMLHttpRequest (); 
  Var url = "ProgressBarServlet? Poll & task = key =" + key; 
  XmlHttp.open ( "GET", url, true); 
  XmlHttp.onreadystatechange = pollCallback; 
  XmlHttp.send (null); 
  ) 

  Function pollCallback () ( 
  If (xmlHttp.readyState == 4) ( 
  If (xmlHttp.status == 200) ( 
  Var percent_complete = xmlHttp.responseXML.getElementsByTagName ( "percent") [0]. FirstChild.data; 
  Var progress = document.getElementById ( "progress"); 
  Var progressPersent = document.getElementById ( "progressPersent"); 
  Progress.width percent_complete + = "%"; 
  ProgressPersent.innerHTML percent_complete + = "%"; 
  If (percent_complete <100) ( 
  SetTimeout ( "pollServer ()", 2000); 
  Else () 
  Document.getElementById ( "complete"). InnerHTML = "Complete!"; 
  / / Document.getElementById ( "go"). Disabled = false; 
  ) 
  ) 
  ) 
  ) 
  Function clearBar () ( 
  Var progress_bar = document.getElementById ( "progressBar"); 
  Var progressPersent = document.getElementById ( "progressPersent"); 
  Var complete = document.getElementById ( "complete"); 
  Progress_bar.style.visibility = "visible" 
  ProgressPersent.innerHTML = ""; 
  Complete.innerHTML = "Begin to upload this file ..."; 
  ) 
  </ Script> 
  </ Head> 
<body>
  <div Id="progressBar" style="padding:0px;border:solid black 0px;visibility:hidden"> 
  <table Width="300" border="0" cellspacing="0" cellpadding="0" align="center"> 
<tr>
  <td Align="center" id="progressPersent"> 86% </ td> 
  </ Tr> 
  <tr> 
<td>
  <table Width="100%" border="1" cellspacing="0" cellpadding="0" bordercolor="#000000"> 
<tr>
<td>
  <table Width="1%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FF0000" id="progress"> 
<tr>
  <td> </ Td> 
  </ Tr> 
  </ Table> </ td> 
  </ Tr> 
  </ Table> 
  </ Td> 
  </ Tr> 
<tr>
  <td Align="center" id="complete"> completed </ td> 
  </ Tr> 
  </ Table> 
  </ Div> 
  <input id = "go" name="run" type="button" value="run" onClick="go();"> 

  </ Body> 
  </ Html> 


  2: a simulated servlet: ProgressBarServlet1.    Java, as follows: 
  Package com.cyberobject.lcl.ajax; 

  Import java.io. *; 

  Import javax.servlet .*; 
  Import javax.servlet.http .*; 

  / ** 
*
  * @ Author nate 
  * @ Version 
  * / 
  Public class ProgressBarServlet extends HttpServlet ( 
  Private int counter = 1; 

  / ** Handles the HTTP <code> GET </ code> method. 
  * @ Param request servlet request 
  * @ Param response servlet response 
  * / 
  Protected void doGet (HttpServletRequest request, HttpServletResponse response) 
  Throws ServletException, IOException ( 
  String task = request.getParameter ( "task"); 
  String res = ""; 

  If (task.equals ( "create")) ( 
  Res = "<key> 1 </ key>"; 
  Counter = 1; 
  ) 
  Else ( 
  String percent = ""; 
  Switch (counter) ( 
  Case 1: percent = "10"; break; 
  Case 2: percent = "23"; break; 
  Case 3: percent = "35"; break; 
  Case 4: percent = "51"; break; 
  Case 5: percent = "64"; break; 
  Case 6: percent = "73"; break; 
  Case 7: percent = "89"; break; 
  Case 8: percent = "100"; break; 
  ) 
  Counter + +; 

  Res = "<percent>" + + percent "</ percent>"; 
  ) 

  PrintWriter out = response.getWriter (); 
  Response.setContentType ( "text / xml"); 
  Response.setHeader ( "Cache-Control", "no-cache"); 
  Out.println ( "<response>"); 
  Out.println (res); 
  Out.println ( "</ response>"); 
  Out.close (); 
  ) 

  / ** Handles the HTTP <code> POST </ code> method. 
  * @ Param request servlet request 
  * @ Param response servlet response 
  * / 
  Protected void doPost (HttpServletRequest request, HttpServletResponse response) 
  Throws ServletException, IOException ( 
  DoGet (request, response); 
  ) 

  / ** Returns a short description of the servlet. 
  * / 
  Public String getServletInfo () ( 
  Return "Short description"; 
  ) 
  ) 
  3: In the web.    Xml configuration in good servlet mapping: 
  <! - Action Servlet Mapping -> 
<servlet>
  <servlet-name> ProgressBarServlet </ servlet-name> 
  <display-name> ProgressBarServlet </ display-name> 
  <servlet-class> Com.cyberobject.lcl.ajax.ProgressBarServlet </ servlet-class> 
  </ Servlet> 

<servlet-mapping>
  <servlet-name> ProgressBarServlet </ servlet-name> 
  <url-pattern> / ProgressBarServlet </ url-pattern> 
  </ Servlet-mapping> 

  At this time of the progress already running.    The next work is to transplant it to our system. 
  2: 
  1: writing class DbOperater database, add a progress attribute 
  Private int progress; 

  2: writing database class, add a getProgress () method: 
  Public int getProgress () 
  ( 
  Return progress; 
  ) 
  3: the writing for the cycle, progress + +; 
  4: Calling the servlet DbOperater Calling DbOperater the getProgress () method, such as on the progress of the provision of real-time data. 
  5: Also: servlet the doGet () to access the progress of data, doPost () to upload files and the write operation. Clear division of work and each other. 
  Hereby filing. 




Related articles:

Courseware in Flash SWF files in the correct call
  As we all know, Flash SWF output document is the standard format of the document, which are widely used in PowerPiont, Authorware, and other software can be easily called.    How in the use of Flash SWF files?    This paper will detail all related technologies.  &nb...
Fireworks produced by the dynamic effects of LOGO
  This case against a major hidden LOGO gradual effect on the cases carefully to the FW cover all the major animation techniques for beginners may be a bit difficult, but as long as we tried to figure out carefully, with fw do gif can fully grasp    1, a new 88 * 31 of the canvas, t...
Several css basic skills
  1. Css font simplified rules    When using css definition of fonts, you may do so: font-size: 1em;    Line-height: 1.5em;    Font-weight: bold;    Font-style: italic;    Font-variant: small-caps;    Font-family: verdana, serif;...
Ajax error handling mechanism Technology
  AJAX framework is the core component XMLHttpRequest JavaScript objects, which allows developers to the client without disrupting user operation, should not use hidden pages circumstances, send and receive via HTTP XML documents.    Now, some people might feel fear, because it allows...
Application of Advanced HTML skills (1) language based on HTML
  First, why should learn HTML?    During the past two years, many companies developed the HTML graphical development tools, making the production website has become very simple.    If Microsoft introduced Microsoft FrontPage, Adobe introduced Adobe Pagemill, Micromedia Co...