The life cycle and how the servlet works

Source: Internet
Author: User
Tags http request

The servlet life cycle is divided into three phases:
1: Initialize phase, call Init () method
2: Call the Service () method in response to the customer request phase
3: Terminate phase, call Destory () method

The life cycle of 1:servlet refers to the entire life process from which a servlet is loaded into a Web server and destroyed.

After the Web server loads the Servlet:web server starts, It will find the servlet for the project based on the Web. xml file for each project, and find the address of the class files for those servlets, load them into the Web container, and the different servlets are loaded in a different order by the website, and you can configure each servlet The order in which the load is loaded. In general, the servlet is loaded by the Web server when the server is started, and the JSP file is dynamically loaded only when the user accesses the page.

Create a Servlet instance: This will invoke the Servlet's constructor to create a specific object.
After the two steps above, if there is a client request, the Web server calls the Servlet object's init () method
The Web server then invokes the servlet's service () method to receive the request, process the request, and return the processing results.
Destroy: The servlet instance is destroyed, which is accomplished by invoking the Destory () method of the servlet.

In the above stages, the servlet provides the most important service phase. The service () method is a real concern for programmers, because it is the way that the servlet really starts responding to client requests and processes the business logic. After the Service () receives the client request, it calls the servlet's Doget () method or the Dopost () method to process the request. So when writing your own servlet, it is generally only necessary to rewrite the doget () and Dopost () methods to process the client request and return the processing results.


steps:
1:web client makes an HTTP request to the servlet container (Tomcat)
2:servlet container receives Web client request
The 3:servlet container creates a HttpRequest object that encapsulates the information requested by the Web client into this object.
4:servlet container to create a HttpResponse object
The 5:servlet container invokes the service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as parameters to the HttpServlet object.
6:httpservlet calls the HttpRequest object's methods to get the HTTP request information
7:httpservlet calls the methods of the HttpResponse object to generate the response data
The 8:servlet container passes the results of the HttpServlet response to the Web Client.

working principle:

The process of receiving and responding to a client request is explained first, the client sends a request, the servlet container creates the ServletRequest object and the Servletresponse object that is specific to the request, and then invokes the servlet's service () Method. The Service () method obtains the customer request information from the ServletRequest object, processes the request, and then returns the response information to the customer through the Servletresponse object.

time to create a Servlet object

1:servlet container Startup: Reads information from the Web. XML configuration file, constructs the specified Servlet object, creates a ServletConfig object, and invokes the ServletConfig object as a parameter to invoke the Init method of the Servlet object.
2: After the servlet container is started: The client makes a request to the servlet for the first time, the servlet container will determine if there is a specified Servlet object in memory, create it if not, and then create a httprequest based on the customer's request. A HttpResponse object that invokes the service method of the Servlet object.
The 3:servlet servlet container automatically creates a servlet at startup, which is determined by the <load-on-startup> property set for the servlet in the Web. xml file. From there, we can see that the same type of servlet object exists as a singleton 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>

Reference blog:
Http://www.cnblogs.com/xuekyo/archive/2013/02/24/2924072.html

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.