Detailed analysis of the design example of the progress bar in JSP programming (1)

Source: Internet
Author: User
Tags sleep
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 ()
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.