JSP编程进度条设计实例_MySQL
许多Web应用、企业应用涉及到长时间的操作,例如复杂的数据库查询或繁重的XML处理等,虽然这些任务主要由数据库系统或中间件完成,但任务执行的结果仍旧要借助JSP才能发送给用户。本文介绍了一种通过改进前端表现层来改善用户感觉、减轻服务器负载的办法。
当JSP调用一个必须长时间运行的操作,且该操作的结果不能(在服务器端)缓冲,用户每次请求该页面时都必须长时间等待。很多时候,用户会失去耐心,接着尝试点击浏览器的刷新按钮,最终失望地离开。
本文介绍的技术是把繁重的计算任务分离开来,由一个独立的线程运行,从而解决上述问题。当用户调用JSP页面时,JSP页面会立即返回,并提示用户任务已经启动且正在执行;JSP页面自动刷新自己,报告在独立线程中运行的繁重计算任务的当前进度,直至任务完成。
一、模拟任务
首先我们设计一个TaskBean类,它实现java.lang.Runnable接口,其run()方法在一个由JSP页面(start.jsp)启动的独立线程中运行。终止run()方法执行由另一个JSP页面stop.jsp负责。TaskBean类还实现了java.io.Serializable接口,这样JSP页面就可以将它作为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包含的“繁重任务”是计算1+2+3…+100的值,不过它不通过100*(100+1)/2=5050公式计算,而是由run()方法调用work()方法100次完成计算。work()方法的代码如下所示,其中调用Thread.sleep()是为了确保任务总耗时约10秒。
protected void work() {
try {
Thread.sleep(sleep);
counter++;
sum += counter;
} catch (InterruptedException e) {
setRunning(false);
}
}
status.jsp页面通过调用下面的getPercent()方法获得任务的完成状况:
public synchronized int getPercent() {
return counter;
}
如果任务已经启动,isStarted()方法将返回true:
public synchronized boolean isStarted() {
return started;
}
如果任务已经完成,isCompleted()方法将返回true:
public synchronized boolean isCompleted() {
return counter == 100;
}
如果任务正在运行,isRunning()方法将返回true:
public synchronized boolean isRunning() {
return running;
}
SetRunning()方法由start.jsp或stop.jsp调用,当running参数是true时。SetRunning()方法还要将任务标记为“已经启动”。调用setRunning(false)表示要求run()方法停止执行。
public synchronized void setRunning(boolean running) {
this.running = running;
if (running)
started = true;
}
任务执行完毕后,调用getResult()方法返回计算结果;如果任务尚未执行完毕,它返回null:
public synchronized Object getResult() {
if (isCompleted())
return new Integer(sum);
else
return null;
}
当running标记为true、completed标记为false时,run()方法调用work()。在实际应用中,run()方法也许要执行复杂的SQL查询、解析大型XML文档,或者调用消耗大量CPU时间的EJB方法。注意“繁重的任务”可能要在远程服务器上执行。报告结果的JSP页面有两种选择:或者等待任务结束,或者使用一个进度条。
public void run() {
try {
setRunning(true);
while (isRunning() && !isCompleted())
work();
} finally {
setRunning(false);
}
}
二、启动任务
start.jsp是web.xml部署描述符中声明的欢迎页面,web.xml的内容是:
br> PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
start.jsp启动一个专用的线程来运行“繁重的任务”,然后把HTTP请求传递给status.jsp。
start.jsp页面利用
下面是start.jsp页面的代码清单:
start.jsp创建并设置好TaskBean对象之后,接着创建一个Thread,并将Bean对象作为一个Runnable实例传入。调用start()方法时新创建的线程将执行TaskBean对象的run()方法。
现在有两个线程在并发执行:执行JSP页面的线程(称之为“JSP线程”),由JSP页面创建的线程(称之为“任务线程”)。接下来,start.jsp利用调用status.jsp,status.jsp显示出进度条以及任务的执行情况。注意status.jsp和start.jsp在同一个JSP线程中运行。
start.jsp在创建线程之前就把TaskBean的running标记设置成了true,这样,即使当JSP线程已开始执行status.jsp而任务线程的run()方法尚未启动,也能够确保用户会得到“任务已开始运行”的状态报告。
将running标记设置成true、启动任务线程这两行代码可以移入TaskBean构成一个新的方法,然后由JSP页面调用这个新方法。一般而言,JSP页面应当尽量少用Java代码,即我们应当尽可能地把Java代码放入Java类。不过本例中我们不遵从这一规则,把new Thread(task).start()直接放入start.jsp突出表明JSP线程创建并启动了任务线程。
在JSP页面中操作多线程必须谨慎,注意JSP线程和其它线程实际上是并发执行的,就象在桌面应用程序中,我们用一个线程来处理GUI事件,另外再用一个或多个线程来处理后台任务。不过在JSP环境中,考虑到多个用户同时请求某一个页面的情况,同一个JSP页面可能会在多个线程中同时运行;另外,有时同一个用户可能会向同一个页面发出多个请求,虽然这些请求来自同一个用户,它们也会导致服务器同时运行一个JSP页面的多个线程。
三、任务进度
status.jsp页面利用一个HTML进度条向用户显示任务的执行情况。首先,status.jsp利用
为了及时反映任务执行进度,status.jsp会自动刷新。JavaScript代码setTimeout("location='status.jsp'", 1000)将每隔1000毫秒刷新页面,重新请求status.jsp,不需要用户干预。
进度条实际上是一个HTML表格,包含10个单元――即每个单元代表任务总体的10%进度。
JSP进度条
结果:
%
BORDER=1 CELLPADDING=0 CELLSPACING=2>
percent; i -= 10) { %>
任务执行情况分下面几种状态:正在执行,已完成,尚未开始,已停止:
页面底部提供了一个按钮,用户可以用它来停止或重新启动任务:
正在执行
完成
尚未开始
已停止
只要不停止任务,约10秒后浏览器将显示出计算结果5050:
四、停止任务
stop.jsp页面把running标记设置成false,从而停止当前的计算任务:
注意最早的Java版本提供了Thread.stop方法,但JDK从1.2版开始已经不赞成使用Thread.stop方法,所以我们不能直接调用Thread.stop()。
第一次运行本文程序的时候,你会看到任务的启动有点延迟;同样地,第一次点击“停止”按钮时也可以看到任务并没有立即停止运行(特别是如果机器配置较低的话,延迟的感觉更加明显),这些延迟都是由于编译JSP页面导致的。编译好JSP页面之后,应答速度就要快多了。
五、实际应用
进度条不仅使得用户界面更加友好,而且对服务器的性能也有好处,因为进度条会不断地告诉用户当前的执行进度,用户不会再频繁地停止并重新启动(刷新)当前的任务。另一方面,创建单独的线程来执行后台任务也会消耗不少资源,必要时可考虑通过一个线程池来实现Thread对象的重用。另外,频繁地刷新进度页面也增加了网络通信开销,所以务必保持进度页面简洁短小。
在实际应用中,后台执行的繁重任务可能不允许停止,或者它不能提供详细的执行进度数据。例如,查找或更新关系数据库时,SQL命令执行期间不允许中途停止――不过如果用户表示他想要停止或中止任务,程序可以在SQL命令执行完毕后回退事务。
解析XML文档的时候,我们没有办法获知已解析内容精确的百分比。如果用DOM解析XML文档,直到解析完成后才得到整个文档树;如果用SAX,虽然可以知道当前解析的内容,但通常不能确定还有多少内容需要解析。在这些场合,任务的执行进度只能靠估计得到。
估计一个任务需要多少执行时间通常是很困难的,因为它涉及到许多因素,即使用实际测试的办法也无法得到可靠的结论,因为服务器的负载随时都在变化之中。一种简单的办法是测量任务每次执行所需时间,然后根据最后几次执行的平均时间估算。如果要提高估计时间的精确度,应当考虑实现一种针对应用特点的算法,综合考虑多种因素,例如要执行的SQL语句类型、要解析的XML模式的复杂程度,等等。
结束语:本文例子显示出用JSP、Java、HTML和JavaScript构造进度条是相当容易的,真正困难的是如何将它用到实际应用之中,特别是获得后台任务的进度信息,但这个问题没有通用的答案,每一种后台执行的任务都有它自己的特点,必须按照具体情况具体分析。 (出处:PConline)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

According to news on May 13, vivoX100s was officially released tonight. In addition to excellent images, the new phone also performs very well in terms of signal. According to vivo’s official introduction, vivoX100s uses an innovative universal signal amplification system, which is equipped with up to 21 antennas. This design has been re-optimized based on the direct screen to balance many signal requirements such as 5G, 4G, Wi-Fi, GPS, and NFC. This makes vivoX100s the mobile phone with the strongest signal reception capability in vivo’s history. The new phone also uses a unique 360° surround design, with antennas distributed around the body. This design not only enhances the signal strength, but also optimizes various daily holding postures to avoid problems caused by improper holding methods.

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

To avoid thread starvation, you can use fair locks to ensure fair allocation of resources, or set thread priorities. To solve priority inversion, you can use priority inheritance, which temporarily increases the priority of the thread holding the resource; or use lock promotion, which increases the priority of the thread that needs the resource.

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

Thread termination and cancellation mechanisms in C++ include: Thread termination: std::thread::join() blocks the current thread until the target thread completes execution; std::thread::detach() detaches the target thread from thread management. Thread cancellation: std::thread::request_termination() requests the target thread to terminate execution; std::thread::get_id() obtains the target thread ID and can be used with std::terminate() to immediately terminate the target thread. In actual combat, request_termination() allows the thread to decide the timing of termination, and join() ensures that on the main line

According to news on May 8, Apple’s new iPad Pro/Air tablets have been released. According to Apple’s official website, the new iPad Pro and iPad Air no longer support the second-generation Apple Pencil released in 2018, but only support Apple Pencil Pro and Apple Pencil (USB- C). Apple Pencil (USB-C) will be released in November 2023. This stylus maintains the same pixel-level accuracy, low latency, and tilt-angle sensing features as the first and second-generation Apple Pencil, while eliminating pressure sensitivity. function and does not support wireless charging. Its price is 649 yuan. And the newly released ApplePe

Xiaomi's Redmi brand is gearing up to add another budget phone to its portfolio - the Redmi 14C. The device is confirmed to be released in Vietnam on August 31st. However, ahead of the launch, the phone's specifications have been revealed via a Vietnamese retailer. Redmi14CR Redmi often brings new designs in new series, and Redmi14C is no exception. The phone has a large circular camera module on the back, which is completely different from the design of its predecessor. The blue color version even uses a gradient design to make it look more high-end. However, Redmi14C is actually an economical mobile phone. The camera module consists of four rings; one houses the main 50-megapixel sensor, and another may house the camera for depth information.
