The life cycle of the servlet

Source: Internet
Author: User

  1. IE connects to a Web server via a URL, such as: Http://www.baidu.com/app/servlet1
  2. IE sends HTTP requests to the server;
  3. the Web server resolves the hostname of IE to access www.baidu.com (first find the Hosts file in the native C:\Windows\System32\drivers\etc directory, see if the corresponding host name has the corresponding IP, no, Then find the corresponding host name in DNS IP);
  4. Web server resolves the Web application to be accessed by IE;
  5. the Web server resolves the resource to be accessed by IE; Servlet1
  6. The Web server creates the Servlet instance object for the first time ( if the servlet has already been instantiated on a previously accessed Servlet,web server, it does not need to be created );
  7. Call the servlet's Init method to complete the initialization of the object;
  8. Create a request representing the requested and a response that represents the response, and then call the Servlet's service method to respond to the client's request;
  9. The service method executes, chooses to execute the doget () or Dopost () method according to the request, and then writes the data to the client for the response object responding on behalf of the clients ( Implemented by Response.getoutputstream (). Write (byte[] Byte);
  10. service return;
  11. The server extracts the data from the response and constructs an HTTP response;
  12. the server responds write back to the client;
  13. The browser parses the HTTP response and extracts the data display.
  14. servlet Destruction: The servlet isbuilt to reside in memory until the Web server stops or the app is deleted, and the servlet is destroyed, and the Destroy () method is called when it is destroyed


It is important to note that:

multiple servlet requests to the client, typically the server creates only one Servlet instance object, that is, once created, it resides in memory and serves subsequent requests until the Web container exits and the servlet instance destroys

The Servlet's Init method is called only once throughout the servlet's life cycle. Each access request to the servlet causes the servlet engine to invoke the service method.
and each time a new HttpServletRequest and HttpServletResponse object is created, and the two objects are passed into the service method as arguments, the service method then decides to call Doget as requested. Dopost and Other methods

if the <load-on-startup> element is configured in the <Servlet> element in the Web. xml file, then it loads and creates an instance object of the Servlet at startup. And the Init method that calls the servlet

<Servlet>    <servlet-name>servlet1</servlet-name>    <servlet-class> Com.boco.servlet.servlet1</servlet-class>    <load-on-startup>2</load-on-startup></ Servlet>



In this way, you can write a initservlet for the web app, which is configured to mount at boot time, creating the necessary database tables and data for the entire Web application
The data in the <load-on-startup> tag is generally a positive integer, and the smaller the higher the priority starts

The life cycle of the 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.