Servlet operating principle and life cycle

Servlet operating principle 

 

 

Servlet life cycle

        Defines how a servlet is loaded, initialized, and how it receives requests, responds to requests, and provides services. Before discussing the servlet life cycle, let's take a look at these methods:

1. init() method

      The init() method is executed only once in the life cycle of a servlet, it is executed when the server loads the servlet, and the server can be configured to load the servlet when the server is started or when the client first accesses the servlet. No matter how many clients access the servlet, init() will not be executed repeatedly;

2. service() method

      It is the core of Servlet. Whenever a client requests an HttpServlet object, the Service() method of the object is called, and a "request" (ServletRequest) object and a "response" (ServletResponse) object are passed to this method as parameters. . The Service() method already exists in HttpServlet. The default service function is to call the do function corresponding to the method of the HTTP request.

3. destroy() method

      It is executed only once, and this method is executed when the server stops and unloads the servlet, which is somewhat similar to the delete method of C++. A servlet may spawn other threads while running the service() method, so make sure that these threads have terminated or completed when the destroy() method is called.

     Let's talk about the life cycle of servlets. The life cycle of servlets is controlled by the servlet container . It begins when it is loaded into the memory of the web server and ends when the servlet is terminated or reloaded. This operation is generally performed dynamically. However, the server usually provides an administrative option to force the loading and initialization of a specific servlet at server startup.

     In the code, the servlet life cycle is defined by the interface javax.servlet.Servlet. All Java  servlets must directly or indirectly implement the javax.servlet.Servlet interface in order to run on the Servlet Engine. The javax.servlet.Servlet interface defines some methods, which are called in a certain order at a certain time during the life cycle of a servlet.

Servlet life cycle

Load and instantiate servlets

Let's take a look at how Tomcat is loaded:

     1. Automatically mounts at startup if the automount option is configured.

     2. When the server starts, the client makes the first request to the servlet.

     3. When the servlet is reloaded.

      When starting the servlet container, the container first looks for a configuration file web.xml, which records the servlets that can provide services. Each servlet is assigned a servlet name, that is, the full class file name of the Java that the servlet actually corresponds to. The servlet container creates an instance for each servlet with the autoload option. Therefore, every servlet class must have a public parameterless constructor .

initialization

      When the servlet is instantiated, the servlet container will call the init method of each servlet to instantiate each instance. After the init method is executed, the servlet is in the "initialized" state. So, once the servlet is instantiated, the init method must be called. Through the servlet is not initialized immediately after startup, but after receiving the request. Pre-initialize the servlet with <load-on-statup> ...... </load-on-statup> in the web.xml file.

      初始化失败后,执行init()方法抛出ServletException异常,Servlet对象将会被垃圾回收器回收,当客户端第一次访问服务器时加载Servlet实现类,创建对象并执行初始化方法。

请求处理

      Servlet 被初始化以后,就处于能响应请求的就绪状态。每个对Servlet 的请求由一个Servlet Request 对象代表。Servlet 给客户端的响应由一个Servlet Response对象代表。对于到达客户机的请求,服务器创建特定于请求的一个“请求”对象和一个“响应”对象。调用service方法,这个方法可以调用其他方法来处理请求。

      Service方法会在服务器被访问时调用,Servlet对象的生命周期中service方法可能被多次调用,由于web-server启动后,服务器中公开的部分资源将处于网络中,当网络中的不同主机(客户端)并发访问服务器中的同一资源,服务器将开设多个线程处理不同的请求,多线程同时处理同一对象时,有可能出现数据并发访问的错误。

      另外注意,多线程难免同时处理同一变量时(如:对同一文件进行写操作),且有读写操作时,必须考虑是否加上同步,同步添加时,不要添加范围过大,有可能使程序变为纯粹的单线程,大大削弱了系统性能;只需要做到多个线程安全的访问相同的对象就可以了。

卸载Servlet

      当服务器不再需要Servlet实例或重新装入时,会调用destroy方法,使用这个方法,Servlet可以释放掉所有在init方法申请的资源。一个Servlet实例一旦终止,就不允许再次被调用,只能等待被卸载。

      Once the servlet is terminated, the servlet instance can be garbage collected and is in an "unloaded" state. If the servlet container is shut down, the servlet will also be unloaded. A servlet instance can only be initialized once, but multiple identical servlet instances can be created. For example, the same servlet can create multiple instances when connecting to different databases according to different configuration parameters.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324693375&siteId=291194637
Recommended