Java Web Background running principle

Source: Internet
Author: User

Java Web background running principleWhen the Web server receives an HTTP request, it will first determine the content of the request-if it is a static web page data, the Web server will handle it itself and generate the response information, and if dynamic data is involved, the Web server will forward the request to the servlet container. At this point the servlet container will find the corresponding servlet instance that handles the request and the result will be sent back to the Web server and returned to the client by the Web server. For the same Servlet,servlet container, a servlet instance is established the first time an HTTP request is received, and then a thread is started. The second time an HTTP request is received, the servlet container does not need to establish the same servlet instance, but instead initiates a second thread to service the client request. So multithreading can not only improve the efficiency of web application execution, but also reduce the burden of Web server system. Rudely explained the request to the container process

Explains the request-to-container-to-servlet cycle process

Text commentary: 1. The client makes a request->web the server forwards to the Web container tomcat;2. The Tomcat main thread responds to a request that is forwarded to the user to create two objects: HttpServletRequest and HttpServletResponse 3. Find the correct servlet,tomcat from the URL in the request to create or assign a thread to it, and pass the two objects created in step 2 to the thread; 4.Tomcat calls the servlet's Servic () method, depending on the request parameter call Doget () or Dopost () method; 5. Assuming an HTTP GET request, the Doget () method generates a static page and combines it into the response object; When the servlet thread ends: Tomcat sends the Response object to the HTTP response back to the client, while deleting the request and response objects. You can understand the life cycle of the servlet: The Servlet class is loaded (corresponding to 3 steps), the servlet is instantiated (3 steps), the Init method is called (corresponding to 3 steps), the service () method is called (corresponding to 4, 5 steps), and the Destroy () method is called (corresponding to 6 steps). Attention:1.When you create a Servlet object: The servlet container starts: reads the 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. After the servlet container is started: The client makes a request to the servlet for the first time, the servlet container determines if there is a specified Servlet object in memory, creates it if not, and then creates a HttpRequest, HttpResponse object based on the client's request. This invokes the service method of the Servlet object. The servlet servlet container automatically creates the servlet at startup, which is determined by the properties 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.2.There are no doget (), DoPost (), and so on in the Servlet interface and Genericservlet, these methods are defined in HttpServlet, but all return error information, so each time we define a servlet, Must implement these methods such as Doget or Dopost. The httpservlet that we often use are inherited from Genericservlet implementations.

Java Web Background running principle

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.