Ajax: progress bar for improving user experience

Source: Internet
Author: User

In a recent project, the upload function uploads a CVS file, parses the file, and writes it to the database.
Because it is often necessary to upload large files, it may take 40 minutes for the customer to complete this function. During this process, there is no prompt on the page, and the user experience is very poor?
Why not use Ajax as a progress bar?
This requirement can be completed in two steps:
I. Write a simple ajax to implement the simplest progress bar function.
2. Transform the progress bar to a progress bar available for the project.

I. Simplest progress bar
1. The client sends a createxmlhttprequest request every 2 seconds to the server and obtains the progress data returned by the server. Based on the data returned by the server, update the width of a table using JavaScript,
This simulates a progress bar.
Progressbar.html. The content is 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? Task = create ";
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? Task = poll & 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 = "& nbsp ;";
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> & nbsp; </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, the content is 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: Web. Configure servlet ing in XML:
<! -- 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>

Now the progress bar is ready to run. The next task is to transplant it to our system.
II:
1: Add a progress attribute to the class dboperater of the database.
Private int progress;

2: add the getprogress () method to the class for database writing:
Public int getprogress ()
{
Return progress;
}
3: In the for loop of the write database, progress ++;
4: Call the getprogress () method of dboperater In the servlet that calls dboperater, which provides real-time data for the progress bar.
5: Servlet's doget () is used to obtain progress data, and dopost () is used to upload files and write database operations.
It is hereby archived.

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.