Tell me about the servlet lifecycle, how it works __ Editor

Source: Internet
Author: User

please pay attention to blog, post-update quality blog, thank you Servlet Lifecycle:

Servlet loading-> instantiation-> service-> destroyed. init (): in the lifecycle of the servlet, only one init () method is executed. It is executed when the server is mounted in the servlet and is responsible for initializing the Servlet object. You can configure the server to enter the servlet into the servlet for the first time on the boot server or client. Regardless of how many clients access the servlet, init () is not repeated. Service ():

It is the core of the servlet, responsible for responding to customer requests. Whenever a client requests a HttpServlet object, the service () method of the object is invoked and passed to the method a request (ServletRequest) object and a response (Servletresponse) object as an argument. The service () method already exists in HttpServlet. The default service function is to invoke the DO function corresponding to the method of the HTTP request. Destroy (): executes this method only once, when the server side stops and the servlet is uninstalled. When the Servlet object exits the lifecycle, it is responsible for releasing the resource that is occupied. A servlet may produce additional threads when it runs the service () method, so it is necessary to confirm that the threads have been terminated or completed when the Destroy () method is invoked.

how the Tomcat and Servlet work:

Step: The Web client creates a HttpRequest object to the servlet container (TOMCAT), which sends the HTTP request servlet container to the request servlet container that receives the Web client, and the Web Client-requested information is encapsulated in this object. The servlet container creates a HttpResponse object servlet container to invoke the service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as parameters to the HttpServlet object. HttpServlet invokes the method of the HttpRequest object to obtain the HTTP request information. HttpServlet invokes the method of the HttpResponse object to generate the response data. The servlet container passes the HttpServlet response results to the Web Client. How the servlet works:

First of all, a brief explanation of the servlet's process of receiving and responding to a customer request is that the customer sends a request, the servlet invokes the service () method to respond to the request, matches the request in the service () method through the source code, and chooses to invoke the Doget , Dopost, and so on, and then into the corresponding method to invoke the logical layer of the method to achieve customer response. There are no doget (), DoPost (), and so on in the Servlet interface and Genericservlet, and these methods are defined in HttpServlet, but they all return error information, so each time we define a servlet, Must implement these methods such as Doget or Dopost.

Each custom servlet must implement the Servlet interface, and five methods are defined in the Servlet interface, of which the more important three methods refer to the servlet lifecycle, respectively, the Init (), service (), Destroy () Method. Genericservlet is a general-purpose servlet that is not specific to any protocol, and it implements the Servlet interface. And HttpServlet inherits from Genericservlet, so HttpServlet also implements the Servlet interface. So we just need to inherit httpservlet when we define the servlet.

servlet interfaces and Genericservlet are not specific to any protocol, and HttpServlet is a class that is specific to the HTTP protocol, so the service () method is implemented in HttpServlet and the request ServletRequest, Servletresponse
Strong into HttpRequest and HttpResponse.

time to create a Servlet object: When the servlet container starts: reads the information in the Web.xml configuration file, constructs the specified Servlet object, creates the ServletConfig object, and invokes the ServletConfig object as a parameter to invoke the Servlet object's Init method. After the servlet container starts: The first time a customer makes a request to the servlet, the servlet container determines whether a specified Servlet object exists in memory, creates it if not, and then creates a HttpRequest, HttpResponse object based on the customer's request. Thus invoking the service method of the Servlet object. Servlet
The servlet container automatically creates a servlet at startup, as determined by the properties set in the Web.xml file for the servlet. From this we can also see that the same type of servlet object exists in a single instance in the Servlet container.

<servlet>
        <servlet-name>Init</servlet-name>
        <servlet-class> org.xl.servlet.initservlet</servlet-class>
        <load-on-startup>1</load-on-startup>
< /servlet>

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.