JSP programming progress bar design instance _ MySQL

Source: Internet
Author: User
Many Web applications and enterprise applications involve long-time operations, such as complex database queries or heavy XML processing. although these tasks are mainly completed by the database system or middleware, however, the results of task execution can still be sent to the user through JSP. This article introduces a method to improve user perception and reduce server load by improving the front-end presentation layer. When JSP calls
Many Web applications and enterprise applications involve long-time operations, such as complex database queries or heavy XML processing. although these tasks are mainly completed by the database system or middleware, however, the results of task execution can still be sent to the user through JSP. This article introduces a method to improve user perception and reduce server load by improving the front-end presentation layer. When JSP calls an operation that must run for a long time and the result of the operation cannot be buffered (on the server side), the user must wait for a long time each time when requesting the page. Most of the time, the user will lose patience, and then try to click the refresh button of the browser, and finally leave with disappointment. The technology introduced in this article is to separate heavy computing tasks and run them in an independent thread to solve the above problems. When a user calls a JSP page, the JSP Page returns immediately and prompts that the user task has been started and is being executed. the JSP page automatically refreshes itself, report the current progress of a heavy computing task running in an independent thread until the task is completed.
I. simulate a task
First, we design a TaskBean class that implements the java. lang. Runnable interface. its run () method runs in an independent thread started by the JSP page (start. jsp. The execution of the run () method is the responsibility of stop. JSP on another jsp page. The TaskBean class also implements the java. io. Serializable interface, so that the JSP page can call it as a JavaBean:
Package test. barBean;
Import java. io. Serializable;
Public class TaskBean implements Runnable, Serializable {
Private int counter;
Private int sum;
Private boolean started;
Private boolean running;
Private int sleep;
Public TaskBean (){
Counter = 0;
Sum = 0;
Started = false;
Running = false;
Sleep = 100;
}
}
TaskBean's "heavy task" is computing 1 + 2 + 3... The value of + 100 is calculated by calling the work () method 100 times instead of the formula 100*(5050 + 1)/2 = 100. The code for the work () method is as follows, in which Thread. sleep () is called to ensure that the total task time is about 10 seconds.
Protected void work (){
Try {
Thread. sleep (sleep );
Counter ++;
Sum + = counter;
} Catch (InterruptedException e ){
SetRunning (false );
}
}

The status. jsp page obtains the task completion status by calling the following getPercent () method:
Public synchronized int getPercent (){
Return counter;
} If the task has been started, the isStarted () method returns true:
Public synchronized boolean isStarted (){
Return started;
}

If the task has been completed, the isCompleted () method returns true:
Public synchronized boolean isCompleted (){
Return counter = 100;
}

If the task is running, the isRunning () method returns true:
Public synchronized boolean isRunning (){
Return running;
}

The SetRunning () method is called by start. jsp or stop. jsp. when the running parameter is true. The SetRunning () method also marks the task as "started ". Calling setRunning (false) indicates that the run () method is required to stop execution.
Public synchronized void setRunning (boolean running ){
This. running = running;
If (running)
Started = true;
}

After the task is completed, call the getResult () method to return the calculation result. if the task has not been completed, it returns null:
Public synchronized Object getResult (){
If (isCompleted ())
Return new Integer (sum );
Else
Return null;
}

When running is marked as true and completed is marked as false, the run () method calls work (). In practical applications, the run () method may need to execute complicated SQL queries, parse large XML documents, or call the EJB method that consumes a lot of CPU time. Note that "heavy tasks" may be executed on remote servers. The JSP page of the report result has two options: wait for the task to end, or use a progress bar.
Public void run (){
Try {
SetRunning (true );
While (isRunning ()&&! IsCompleted ())
Work ();
} Finally {
SetRunning (false );
}
} Ii. start the task

Start. jsp is the welcome page stated in the web. xml deployment descriptor. the content of web. xml is:

PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/dtd/web-app_2_3.dtd>


Start. jsp


Start. jsp starts a dedicated thread to run "heavy tasks", and then passes the HTTP request to status. jsp.
Start. jsp page exploitation Mark the instance for creating a TaskBean and define the scope attribute as session so that other pages can be extracted to the same Bean object for HTTP requests from the same browser. Start. jsp ensures that Create a new Bean object instead of extracting an old object (for example, the Bean object created on the JSP page earlier in the same user session ).
The following is a list of codes on the start. jsp page:
<% Session. removeAttribute ("task"); %>

Class = "test. barBean. TaskBean"/>
<% Task. setRunning (true); %>
<% New Thread (task). start (); %>


After start. jsp creates and sets the TaskBean object, it creates a Thread and passes in the Bean object as a Runnable instance. When the start () method is called, the newly created thread executes the run () method of the TaskBean object.
Now there are two threads running concurrently: the thread that executes the JSP page (called the "JSP thread"), the thread created by the JSP page (called the "task thread "). Next, start. jsp uses the call status. jsp and status. jsp to display the progress bar and task execution status. Note that status. jsp and start. jsp run in the same JSP thread.
Start. before creating a thread, jsp sets the running mark of TaskBean to true, even if the JSP thread has started to execute status. jsp and the run () method of the task thread has not been started. It can also ensure that the user will receive the status report "The task has started running.
Set the running flag to true and start the task thread. These two lines of code can be moved into TaskBean to form a new method, which is then called on the JSP page. Generally, JSP pages should use Java code as little as possible, that is, we should put Java code into Java classes as much as possible. However, in this example, we do not follow this rule and put new Thread (task). start () directly into start. jsp to highlight that the JSP Thread has created and started the task Thread.
Be cautious when operating multithreading on JSP pages. Note that JSP threads and other threads are actually executed concurrently, just as in a desktop application, we use a thread to process GUI events, in addition, one or more threads are used to process background tasks. However, in the JSP environment, considering that multiple users request a page at the same time, the same JSP page may run simultaneously in multiple threads. In addition, sometimes the same user may send multiple requests to the same page. although these requests come from the same user, they also cause the server to run multiple threads on one JSP page at the same time. III. task progress
The status. jsp page displays the execution status of the task to the user using an HTML progress bar. First, use status. jsp Tag to obtain the Bean object created on the start. jsp page:

Class = "test. barBean. TaskBean"/>
To timely reflect the task execution progress, status. jsp will be automatically refreshed. The JavaScript code setTimeout ("location = 'status. jsp '", 1000) refresh the page every 1000 milliseconds and request status. jsp again without user intervention.


JSP progress bar
<% If (task. isRunning () {%>

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.