Origin
in the explanation Servlet you need to first introduce a word CGI that is, common gatewayinterface is the Universal Gateway Interface, it provides a computer program with the HTTP protocol or the WWW service interface, that is, the meaning of human-computer interface. The Web application runs on the server, and the client's request, just beginning, is to implement the data on the web through a CGI program, but for each request made by the client, the CGI must be created A new instance of the program, which will consume a lot of memory, is to solve this problem, our Servlet came into being .
Servlet is a Java write an application that runs on the server, processes the requested information and sends it to the client, so its name is Server+applet. For multiple requests from the client,the servlet instance needs to be created once, thus saving a lot of memory, andtheservlet resides in memory after initialization. Therefore, there is no need to re-instantiate each request.
Application Architecture
Servlet Container will Servlet dynamically loaded onto the server, HttpServlet interacts with the client using HTTP requests and HTTP responses. Therefore, the Servlet container supports the request and the corresponding HTTP protocol. Servlet Application Architecture:
Indicates that the client's request to the servlet is first received by the HTTP server, which submits the client's HTTP request to the servlet container, and the servlet container invokes the response of the corresponding servlet,servlet to the servlet container. And, in turn, the HTTP server transmits the response to the client. The HTTP server provides static content and passes all client requests made to the servlet to the servlet container.
In the previous article we learned about Omcat, a small, lightweight application server that is widely used in small and medium-sized systems and concurrent users, with the ability to process HTML like IIS and Apache, and we have learned that It is also a container for servlets and JSPs, for which Tomcat is a combination of the HTTP server and the two parts of the servlet container.
Life cycle
The life cycle of a servlet is as follows:
The servlet is run in a servlet container and its lifecycle is managed by the container. The life cycle of the servlet is represented by the Init (), service (), and Destroy () methods in the Javax.servlet.Servlet interface.
Loading and instantiating
The servlet container is responsible for loading and instantiating the servlet, which is typically performed dynamically. The servlet instance is created when the servlet container is started, or when the container detects that the servlet is needed to respond to the first request. Below we describe how the container is dynamically instantiating the servlet, and when the servlet container starts, it must know where the servlet class is needed, and the servlet container loads the servlet class through the ClassLoader, and after the successful load, the container creates an instance of the servlet. Because the container is creating a servlet instance through the Java Reflection API, invoking the default constructor method of the servlet (that is, the constructor without parameters), we should not provide a constructor with parameters when writing the Servlet class.
Initialize
The container initializes the object (configured under the <servlet> tab in Web. xml) by calling the Servlet's init () method. The purpose of initialization is to have the Servlet object do some initialization work before processing the client request, such as establishing a connection to the database, obtaining configuration information, and so on. For each servlet instance, the init () method is called only once.
Request Processing
The servlet container invokes the servlet's service () method to process the request, passing the request and response object as parameters, the service () method getting information about the requested object, processing the request, accessing other resources, obtaining the required information, service () Method uses the method of the response object to pass the response back to the server and eventually to the client. The service () method may activate other methods to process the request, such as doget () or dopost () or a new method developed by the programmer himself.
Termination of Service
When the container detects that a servlet instance should be removed from the service (typically when the server shuts down), the container invokes the instance's destroy () method so that the instance can free the resources it uses. After the Destroy () method call, the container releases the servlet instance, which is then reclaimed by the Java garbage collector. If the servlet is required to process the request again, the servlet container creates a new servlet instance.
Note
During the lifetime of the servlet, the creation of the servlet instance, the init (), and Destroy () methods of the calling instance are done only once, and when the initialization is complete, the servlet container saves the instance in memory by invoking its service () method, Service for the received request.
Summarize
The use of Servlets will enable us to develop systems that are more portable, improve our system performance, and effectively safeguard the security issues, regarding the use of servlets in the project, in the following articles we will continue to introduce.
Java DRP Project Combat--servlet