JSP programming progress bar design example (3)

Source: Internet
Author: User

Ii. Start the task


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


<? Xml version = "1.0" encoding = "GB2312"?>


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


<Web-app>
<Welcome-file-list>
<Welcome-file> start. jsp </welcome-file>
</Welcome-file-list>
</Web-app>


Start. jsp starts a dedicated thread to run "heavy tasks", and then passes the HTTP request to status. jsp.


Start. the jsp page uses the <jsp: useBean> tag to create a TaskBean instance and defines the scope attribute as a session so that other pages can be extracted to the same Bean object for HTTP requests from the same browser. Start. jsp calls the session. removeAttribute ("task") ensures that <jsp: useBean> creates 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"); %>


<Jsp: useBean id = "task" scope = "session"
Class = "test. barBean. TaskBean"/>


<% Task. setRunning (true); %>
<% New Thread (task). start (); %>
<Jsp: forward page = "status. jsp"/>
 
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.


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.