Servlet lifecycle, how it works

Source: Internet
Author: User
Tags java web
One: Servlet lifecycle, Working principle

First understand the process of the Java Web application


1.2 What is a servlet.


1.3Servlet Operation principle


1, first of all, a brief explanation of the servlet to receive and respond to customer requests, the first customer to send a request, the servlet is called the Service () method to respond to the request, through the source code is visible, the service () method in the way the request is matched, Choose to invoke these methods such as Doget,dopost, and then enter the corresponding method to invoke the logical layer of the method to achieve the customer's 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.

2. Each custom servlet must implement the Servlet interface, and five methods are defined in the Servlet interface, in which the more important three methods relate to the lifecycle of the servlet, 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.

3. The servlet interface 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.

1.4servlet life cycle


Servlet lifecycle: servlet load---> Instantiation---> Service---> Destroy. 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.

1.5Tomcat and how the Servlet works:

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.

1.6 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. The servlet servlet container automatically creates a servlet at startup, as determined by the <load-on-startup> attribute 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>
original link

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.