1. What is Servlet?
The servlet discussed here should be javax. servlet. HTTP. httpservlet, called after the server receives the request, is used to respond to the HTTP request and provides the doget () and dopost () methods in response to the request. it is essentially a Java class, but it is a Java class running on the server. it is independent of platform and protocol and can generate dynamic web pages. it serves as the intermediate layer between customer requests (Web browsers or other HTTP client programs) and server responses (databases or applications on the HTTP server.
The Java Servlet API defines a standard interface between a Servlet and a Java-enabled server, which makes servlets a cross-server platform feature. By creating a framework, Servlet extends the server's capabilities to provide web-based request and response services. When the client sends a request to the server, the server can send the request information to the servlet, and let the servlet establish a response from the server to the client. The servlet can be automatically loaded when the web server or client requests the service for the first time. After loading, the servlet continues to run until other clients send a request.
Ii. servlet Lifecycle
First:
B) entire life process
I. Load servlet, instantiate (new), and initialize (init)
When the servlet is requested to be loaded for the first time, the server initializes the servlet to create a servlet object, which calls the init method to complete the necessary initialization work.
Ii. Request Processing (Service → doget/dopost)
The generated servlet object then calls the service method to respond to the customer's request. Based on the request, the service calls doget () or dopost () for processing.
Iii. Destroy)
When the server is shut down, call the destroy method to destroy the servlet object.
C) Notes
I. The servlet is instantiated only once.
II. The init method is also executed only once.
Iii. servlet is NOT thread-safe
That is, the init method is called only once, that is, the method is called when the servlet is loaded for the first time. When a customer requests the servlet service in the future, the Web service starts a new thread in which the servlet calls the service method to respond to the customer's request, that is, each customer's request causes the service method to be called for execution.
Iii. Differences between Servlet classes and common Java classes
Servlet is a Java application on the server inside the Web server. Unlike the traditional Java application started from the command line, servlet is loaded by the Web server, the Web server must contain a Java virtual machine that supports servlet. The following is a simple comparison between the servlet class and the common Java class:
Iv. Summary
To learn new knowledge, we always try our best to make an analogy with the existing old knowledge. If we are more advanced, we can directly explain it in a living language. Soon after I learned Java, I could only look for the shadows from the current knowledge. The figure below shows the future of servlet learning. net and Java, please do not hesitate to advise:
Although they belong to different platforms and use different languages, they are similar in nature ,. Draw a gourd Based on the samples. It will be much faster if you get started later. This blog is limited to the simple understanding of servlet. Next I will use a small example to illustrate the basic principles of servlet ~