A button is provided at the bottom of the page, which can be used to stop or restart a task:
<TR>
<Td align = "CENTER">
<BR>
<% If (task. isRunning () {%>
<Form method = "GET" ACTION = "stop. jsp">
<Input type = "SUBMIT" VALUE = "stop">
</FORM>
<%} Else {%>
<Form method = "GET" ACTION = "start. jsp">
<Input type = "SUBMIT" VALUE = "start">
</FORM>
<% }%>
</TD>
</TR>
</TABLE>
</BODY> </HTML>
As long as the task is not stopped, the browser will display the computing result 5050 in about 10 seconds:
4. Stop a task
On the stop. jsp page, set the running flag to false to stop the current computing task:
<Jsp: useBean id = "task" scope = "session"
Class = "test. barBean. TaskBean"/>
<% Task. setRunning (false); %>
<Jsp: forward page = "status. jsp"/>
Note that the Thread. stop method is provided in the earliest Java version, but JDK does not approve of using the Thread. stop method since version 1.2, so we cannot directly call Thread. stop ().
When you run the program in this article for the first time, you will see a delay in starting the task. Similarly, when you click "stop" for the first time, you can also see that the task is not stopped immediately (especially if the machine configuration is low, the latency is more obvious ), these latencies are caused by compiling JSP pages. After the JSP page is compiled, the response speed is much faster.
V. Practical Application
The progress bar not only makes the user interface more friendly, but also improves the performance of the server, because the progress bar constantly tells the user the current execution progress, the user will no longer stop and restart (refresh) the current task frequently. On the other hand, creating a separate Thread to execute background tasks also consumes a lot of resources. If necessary, you can use a Thread pool to reuse Thread objects. In addition, refreshing the progress page frequently increases the network communication overhead, so make sure that the progress page is concise and short.
In practical applications, heavy tasks executed in the background may not be stopped, or they cannot provide detailed execution progress data. For example, when you search for or update a relational database, you cannot stop the SQL command during execution. However, if the user indicates that he wants to stop or stop the task, the program can roll back the transaction after the SQL command is executed.
When parsing XML documents, we cannot know the precise percentage of parsed content. If you use DOM to parse an XML document, the entire document tree will not be obtained until the parsing is complete. If you use SAX, although you can know the content to be parsed, you cannot determine the amount of content to be parsed. In these cases, the task execution progress can only be estimated.
It is usually difficult to estimate how much execution time a task needs, because it involves many factors, that is, the method of using actual tests cannot be reliably concluded, because the server load is changing at any time. A simple method is to measure the time required for each task execution, and then estimate the average time of the last few executions. If you want to improve the accuracy of the estimated time, you should consider implementing an algorithm based on the characteristics of the application, taking into account a variety of factors, for example, the type of SQL statement to be executed, the complexity of the XML mode to be parsed, and so on.
Conclusion: The example in this article shows that it is quite easy to construct a progress bar using JSP, Java, HTML, and JavaScript. What is really difficult is how to apply it to practical applications, in particular, the progress information of background tasks is obtained, but there is no general answer to this question. Each background task has its own characteristics and must be analyzed according to the specific situation.