The use of the progress bar is already very common during the installation and downloading of applications. A progress bar can be used to identify the progress of a project, can be expressed as a percentage or a number, and can be placed horizontally. Using AJAX technology to create a progress bar, the function appears more powerful fast.
Create an instance now to demonstrate the implementation of the progress bar using AJAX technology. The instance can also be divided into client code and server-side code.
1, server-side code
service-side code basically implements a client's request information, returning the corresponding percentage number. Open Notepad and enter the following code:
<%@ page contenttype= "text/html;
charset=gb2312 "language=" java "import=" java.sql.* "errorpage=" "%> <%!
int counter = 1;//Note: Multiple users will share this variable, which is only suitable for single user%> <% String task = request.getparameter ("task");
String res = "";
if (Task.equals ("create")) {res = "1";
counter = 1;
else {String percent = "";
Switch (counter) {case 1:percent = "ten"; 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 ();
%>
Saves the above code with the name progressbar.jsp. In the file, you declare a variable counter and assign a value of 1, which is the basis for the percentage number returned by the progress bar. Use the request object below to get the value of the client-delivered variable task, if the value is create, which indicates that the progress bar needs to be recreated and that the counter value is 1, and if the task is not create, the percentage number is returned based on the value of counter. Once the operation is completed, the counter value plus 1.
2, client code
the client code for this instance mainly implements the status of the progress bar based on the returned percentage number. Open Notepad and enter the following code:
Save the above code with the name jspprogressbar.html. In this file, the JavaScript function createxmlhttprequest () is mainly used to create the XMLHttpRequest object, the Go () function completes the server side to send an asynchronous request, the function is called when the Web page is loaded, its main function is to notify the server, and start the progress bar on the client. The Gocallback () function is primarily used to process service-side responses, and every 2 seconds the pollserver () function is also used to send asynchronous requests to the server, primarily requesting the number of servers-side responses. The Pollcallback () function is primarily used to handle server-side responses that specify the display status of the progress bar, based on the number returned by the server. Note here that the Gocallback () function executes only once, and the Pollcallback () function can be executed multiple times, and the remaining three functions are auxiliary functions that implement the progress bar.
3, run
Copy the two JSP files to the JSP directory. The Operation effect chart is as follows: