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 operation principle of Servlet

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

Share

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

This article mainly introduces "what is the operation principle of Servlet". In daily operation, I believe that many people have doubts about the operation principle of Servlet. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what is the operation principle of Servlet?" Next, please follow the editor to study!

1. UML

The following figure is a Servlet UML diagram.

As can be seen from the picture:

Abstract class HttpServlet inherits abstract class GenericServlet, which has two key methods, doGet () and doPost ().

GenericServlet implements interface Servlet,ServletConfig,Serializable

MyServlet (user-defined Servlet class) inherits HttpServlet and overrides the doGet () and doPost () methods of the abstract class HttpServlet

Note: any user-defined Servlet can simply rewrite doPost () and doGet () of the abstract class HttpServlet, such as MyServlet in the figure above.

2. The execution process of Servlet in container

Servlet can be executed only if it is placed in a container, and there are many kinds of Servlet containers, such as Tomcat,WebLogic.

The following figure shows a simple request response model.

Analysis:

The browser sends a GET request to the server (request server ServletA)

The container logic on the server receives the url and determines that it is a Servlet request according to the url. In this case, the container logic will produce two objects: the request object (HttpServletRequest) and the response object (HttpServletResponce).

The container logic finds the target Servlet according to url (in this example, the target Servlet is ServletA), and creates a thread A

The container logic passes the request object and response object just created to thread A.

The container logic calls the service () method of Servlet

The service () method calls the doGet () (this example calls doGet ()) or the doPost () method depending on the type of request (this example is a GET request).

After the execution of doGet (), the result is returned to the container logic

Thread An is destroyed or placed in a thread pool

Note:

There is in principle only one instance of each Servlet in the container

Each request corresponds to one thread

Multiple threads can act on the same Servlet (this is the root cause of Servlet thread unsafety)

Once each thread finishes executing the task, it is destroyed or placed in the thread pool waiting for recycling.

III. The role of Servlet in JavaWeb

Servlet plays two roles in JavaWeb: the page role and the controller role.

With dynamic page technology such as jsp, Servlet pays more attention to the role of controller, and jsp+servlert+model forms a basic three-tier architecture.

(1) Page role of the page

(2) Controller role

Jsp acts as the page role, Servlet acts as the controller role, and the two combine to build a basic MVC three-tier architecture model.

The life cycle of Servlet in the container

The following is a brief overview of the Servlet lifecycle.

Analysis:

* * step: the container loads the Servlet class first

Step 2: instantiate the container Servlet (Servlet no-parameter constructor execution)

Step 3: execute the init () method (only once in the Servlet lifecycle, and before the service () method is executed)

Step 4: execute the service () method to process the customer request, doPost () or doGet ()

Step 5: execute destroy () to destroy the thread

At this point, the study of "how Servlet works" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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