Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of JSP Page Thread

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of JSP page threads. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Simulation task

First we design a TaskBean class that implements the java.lang.Runnable interface and its run () method runs in a separate thread started by the JSP page (start.jsp). It is the responsibility of another JSP page stop.jsp to terminate the execution of the run () method. The TaskBean class also implements the java.io.Serializable interface so that the JSP page can call it as a JavaBean

2. Start the task

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

The start.jsp page uses tags to create an instance of TaskBean and defines the scope attribute as session so that other pages can extract the same Bean object for HTTP requests from the same browser. Start.jsp ensures that a new Bean object is created by calling session.removeAttribute ("task"), rather than extracting an old object (for example, a Bean object created by an earlier JSP page in the same user session)

After start.jsp creates and sets up the TaskBean object, it then 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.

There are now two threads executing concurrently: the thread executing the JSP page (called the "JSP thread") and the thread created by the JSP page (called the "task thread"). Next, start.jsp uses a call to status.jsp,status.jsp to display the progress bar and the execution of the task. Notice that status.jsp and start.jsp are running in the same JSP thread.

Start.jsp sets the running flag of the TaskBean to true before creating the thread, so that even when the JSP thread has started executing the status.jsp and the task thread's run () method has not been started, it ensures that the user will get a status report that the task has started.

The two lines of code that set the running tag to true and start the task thread can be moved into TaskBean to form a new method, which is then called by the JSP page. In general, JSP pages should use as little Java code as possible, that is, we should put Java code into the Java class as much as possible. In this example, however, we don't follow this rule, putting new Thread (task). Start () directly into start.jsp highlights that the JSP thread created and started the task thread.

Multithreading in JSP pages must be handled with caution, noting that JSP threads and other threads actually execute concurrently, just as in desktop applications, we use one thread to handle GUI events and one or more threads to handle background tasks.

However, in a JSP environment, considering that multiple users request a page at the same time, the same JSP page may run in multiple threads at the same time; in addition, sometimes the same user may make multiple requests to the same page, although these requests come from the same user, they also cause the server to run multiple threads of the same JSP page at the same time.

III. Progress of tasks

The status.jsp page uses an HTML progress bar to display the execution of the task to the user. First, status.jsp uses tags to get the Bean object created by the start.jsp page

In order to reflect the progress of task execution in time, status.jsp will refresh automatically. The JavaScript code setTimeout ("location='status.jsp'", 1000) refreshes the page every 1000 milliseconds, re-requesting status.jsp without user intervention.

This is the end of this article on "sample analysis of JSP page threads". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report