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

What is the application method of asynchronous Servlet in AJAX program?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what is the application of asynchronous Servlet in AJAX programs". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the application of asynchronous Servlet in AJAX programs?"

Asynchronous Servlet has also been thinking about the amount of data transferred from the client to the server. From a usability perspective, users get a rich user interface on a thin client browser without having to install anything. However, there is a price to pay when extending these applications on the server side. The number of typical capacity plans for AJAX applications may be three to four times that of standard Web applications.

With this paradigm based on the concept of asynchronous Servlet, one might ask: how does this affect WebLogic Server? Each HTTP request sent to WebLogic uses a thread of execution. Depending on the nature of AJAX programming and the fact that many short-term requests are constantly sent in the form of polling, the behavior pattern may cause a large number of client requests to keep hitting the server. Over the years, WebLogic has taken this issue into account and built a great feature, FutureResponseServlet. Starting with version 6.1, this feature allows developers to provide truly asynchronous notifications from the server without the need for client-side rotational training of events and the use of execution threads on the server side. Before 9.x, BEA was in no hurry to expose the class.

How to make use of this kind in reality? Let's look at an example. Suppose the business requirement is to build an Web-based application that sends data to the server in near real time without refreshing the browser. Such an application can submit a request to the server that takes a long time to process, while still receiving asynchronous events about its state and listening for events. From a technical point of view, there are many ways to achieve this. One way to do this is to use a Java Applet that communicates with Java Servlet to get asynchronous information. This is a good method, but it is a bit inconvenient for users because they have to download a JVM and also download an Applet to the browser. In addition, a persistent socket connection from the client to the server must be maintained to receive asynchronous messages. Imagine that if 1000 users used the Applet, there would be 1000 threads of execution almost empty waiting to send event notifications to the client. Of course, there are other ways, such as building a polling mechanism from Applet or AJAX applications to check for new data on a regular basis. If you don't receive data often, polling is useless, and it wastes server resources and consumes execution threads. Instead, the server can poll periodically, propagate events back to the client, and maintain socket threads without using persistent execution threads. This is very similar to the way Java NIO works. Ideally, we all want to build an application that receives event notifications "asynchronously" from the server without using persistent threads of execution on the server side, whether it's an applet or a thin AJAX-based Web application.

Asynchronous Servlet is particularly present here to face this problem, and one solution is to create a Servlet that extends the FutureResponseServlet class. The browser establishes a single connection to the FutureResponseServlet class and registers itself as a listener in another thread. Whenever an event is received on the server side, the thread notifies the client of the event. The server remains asynchronous with the client, eliminating the need for persistent execution threads. The model can be extended for the case of multiple concurrent users.

This article is not intended to cover how to build AJAX applications. There are already a lot of articles in this area. The focus of this article is on the importance of asynchronous processing in the presentation layer, such as AJAX, Applet, or any front-end application. Listing 1 shows an example.

Import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.Stack; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import weblogic.servlet.FutureResponseServlet; import weblogic.servlet.FutureServletResponse; / / An AsynchronousServlet that handles HTTP requests from a "separate" thread and / / not the execute thread used to invoke this servlet. Public class AsynchronousServerResponseServlet extends FutureResponseServlet {private final Notifier notifier; public AsynchronousServerResponseServlet () {this.notifier = new Notifier (); this.notifier.start ();} public void service (HttpServletRequest request, FutureServletResponse response) throws IOException,ServletException {/ / push this client's request to a buffer and return immediately. / / asynchronous processing occurs in the run method of the Notifier Thread notifier.poll (request, response);} class Notifier extends Thread {private static Stack clients = new Stack (); void poll (HttpServletRequest request, FutureServletResponse response) {clients.push (new Client (request, response));} public void run () {while (! clients.empty ()) {Client client = null; try {client = (Client) clients.pop () PrintWriter pw = client.response.getWriter (); for (int j = 0; j < 10; jacks +) {pw.println ("Time is:" + new Date () + "); pw.flush ();} pw.close ();} catch (Throwable t) {t.printStackTrace ();} finally {try {client.response.send () } catch (IOException ioe) {ioe.printStackTrace ();}} / / inner class that holds Omurn to the clients http request and response class Client {private HttpServletRequest request; private FutureServletResponse response; private Client (HttpServletRequest request, FutureServletResponse response) {this.request = request; this.response = response;}}

As you can see, the example is very simple. The AsynchronousServerResponseServlet class extends FutureResponseServlet and overrides the service method. Only one thread (that is, the Notifier class) is used to handle all client connection responses. For each HTTP request, Servlet registers the socket connection with the Notifier thread and returns. Asynchronous events are delivered to the client, while persistent socket connections are maintained.

A single thread can manage multiple client connections! The run () method can be used to call back events to the client based on a message selection condition. This example performs only one server-side push operation, which is a bit simplistic. Thread pools can be used for some types of event handling.

Asynchronous Servlet applications all in all, FutureResponseServlet is a good feature when dealing with long-running tasks, allowing developers to improve performance, process responses in separate threads, and reduce overhead to *. This approach supports scalability when building asynchronous Servlet applications.

At this point, I believe you have a deeper understanding of "what is the application of asynchronous Servlet in AJAX programs". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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