1, the existence of servlet is to serve customers. The servlet's task is to get a user's request and send back some responses.
- Requests can be complex or simple, for example, "checkout my Shopping Cart," which carries some important data, you must know how to get the data, and, accordingly, the response may need to carry something, and you must know how to write the additional information to the response.
- A servlet can either output a response directly to a Web container or forward a request to a JSP
2. Web containers, such as Tomcat, control the entire life of the servlet.
- The user clicks on a URLthat points to a servlet
- The container "sees" the URL pointing to a servlet, creating two objects:httpservletrequest,httpservletresponse
- The container creates a thread for the corresponding servlet and passes two objects into service (httpservletrequest req, HttpServletResponse resp )
- The service method calls the servlet's corresponding method (doget (httpservletrequest req, HttpServletResponse) based on the HTTP method (GET, post, etc.) sent by the client. Resp),doPost (httpservletrequest req, HttpServletResponse resp) such as
- The servlet responds to the customer by responding to the object, and the container returns the response to the customer.
- The service () thread ends, retains or cleans up the responding object, and the customer gets a response.
3. Mark Tomcat Official document Servlet API documentation
4, HttpServlet inheritance tree, interface
Javax.servlet.GenericServlet
Javax.servlet.http.HttpServlet
Myservlet
- All implemented Interfaces:java.io.Serializable, Servlet, ServletConfig
Servlet interface: Includes three life cycle Methods
Genericservlet: Abstract classes, including most basic servlet behaviors, do not extend this class
HttpServlet: Emphasizing the HTTP features of the servlet
Myservlet: Overwrite the required HTTP methods, such as Dopost, etc.
5. Each request is run on a separate thread (not a process), and theservlet class will not have more than one instance! The servlet class will not have more than one instance!
6. When did the servlet class load and instantiate begin? Basically, when the container starts, there are exceptions, but it must be before the first customer requests a service.
7, before Init (), is only a normal object, when and only if an object has "servlet characteristics", can be called a servlet
8. Between the constructor and Init (), the servlet is in a state between life and death Schrödinger ' s Cat-wikipedia
"Head first Servlets and JSP" NOTE 3