The life cycle and working principle of servlet, use!

Source: Internet
Author: User

Concept:

a servlet is a program that a Java program runs on a server, handles client requests, and does a coarse response!      the servlet is a server component unrelated to the peace platform that runs in a servlet container, and the servlet container

is responsible for the interaction between the servlet and the client and calls the servlet's Dopost, Doget method, using " request "," Response "mode!

The servlet mainly completes the function:

1. Create and return dynamic pages based on client-side requests

2, can be embedded in the existing page

3. Communicate with server resources (database, Java program)

The life cycle of a servlet:

1. Instance, initialize :The servlet container creates a servlet instance and calls the Init () method.

2, respond to the client, request processing : call the Servlet () method (), match the request, select (Doget, DoPost) Method!

3. End of service, termination phase : call the Destroy () method before destroying the instance.

Servlet initialization phase:

 The servlet container loads the servlet at the following times:

The 1,servlet container starts automatically when certain servlets are loaded to implement it only on the web. Add the following code between <Servlet></Servlet> in the XML file:

<loadon-startup>1</loadon-startup>

                2, after the servlet container is started, the customer first sends a request to the servlet

After the 3,servlet class file is updated, after the reload servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization.

The I nit () method is called only once during the entire life cycle of the servlet.

How Servlets work:

  A simple explanation of the servlet's process of receiving and responding to a customer request begins with the client sending a request that the servlet responds to the request by invoking the service () method, which is visible through the source code, matches the request in the service () method, and chooses to call Doget , Dopost and so on, and then into the corresponding method to invoke the logical layer of the method to achieve response to the customer. There are no doget,dopost and so on in the Servlet interface and Genericservlet, these methods are defined in HttpServlet, but all of them 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, which defines five methods in the Servlet interface, of which the more important three methods relate to the servlet's life cycle, which is the above mentioned Init (), service (), Destroy () Method. Genericservlet is a generic, non-protocol-specific servlet that implements the Servlet interface. And HttpServlet inherits from Genericservlet, so HttpServlet also implements the Servlet interface. So we only need to inherit httpservlet when we define the servlet.

Using Servlets:

            1, write a Java class, inherit the HttpServlet class

2. Rewrite init (), doget (), DoPost (), Destroy () method

One, in the Web. xml file Configuration and mapping

          

Second, use @webservlet (name) annotation

          

The life cycle and working principle of servlet, use!

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.