This article shares the life cycle and working principle of Servlet for your reference. The specific content is as follows
The Servlet life cycle is divided into three stages:
1. Call the init() method in the initialization stage
2. Call the service() method in response to customer request stage
3. Call destroy() method in the termination stage
Servlet initialization phase:
At the following moments, the Servlet container loads the Servlet:
1. When the Servlet container starts, some Servlets are automatically loaded. To implement it, you only need to add the following code between <Servlet></Servlet> in the web.XML file:
<loadon-startup>1</loadon-startup>
2. After the Servlet container is started, the client sends a request to the Servlet for the first time
3. After the Servlet class file is updated, reload the Servlet
After the servlet is loaded, the servlet container creates a servlet instance and calls the servlet's init() method for initialization. The init() method is called only once during the entire life of a servlet.
How Servlets work:
First, let’s briefly explain the process of receiving and responding to customer requests. First, the customer sends a request. The Servlet calls the service() method to respond to the request. It can be seen through the source code. The service() method matches the request method. Select to call doGet, doPost and other methods, and then enter the corresponding method to call the logical layer method to realize the response to the customer. There are no methods such as doGet, doPost, etc. in the Servlet interface and GenericServlet. These methods are defined in the HttpServlet, but they all return error information. Therefore, every time we define a Servlet, we must implement methods such as doGet or doPost.
Each custom Servlet must implement the Servlet interface. Five methods are defined in the Servlet interface, among which the three more important methods involve the life cycle of the Servlet, namely the init(), service(), and destroy() methods mentioned above. GenericServlet is a general, not specific to any protocol, Servlet, which implements the Servlet interface. HttpServlet inherits from GenericServlet, so HttpServlet also implements the Servlet interface. So when we define a Servlet, we only need to inherit the HttpServlet.
The Servlet interface and GenericServlet are not specific to any protocol, while HttpServlet is a class specific to the HTTP protocol, so the service() method is implemented in the HttpServlet and the requested ServletRequest and ServletResponse are forced to convert the requested ServletRequest and ServletResponse to HttpRequest and HttpResponse.
public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException{ HttpRequest request; HttpResponse response; try { req = (HttpRequest)request; res = (HttpResponse)response; } catch(ClassCastException e) { throw new ServletException("non-HTTP request response"); } service(request,response);}The code finally calls the HTTPServlet's own service(request, response) method, and then calls the corresponding doXXX method according to the request, because the doXXX method in the HttpServlet returns error information.
protected void doGet(HttpServletRequest res,HttpServletResponse resp) throws ServletException,IOException{ String protocol = req.getProtocol(); String msg = IStrings.getString("http.method_get_not_supported"); if(protocol.equals("1.1")) { resp.sendError(HttpServletResponse.SC.METHOD.NOT.ALLOWED,msg); } esle { resp.sendError(HttpServletResponse.SC_BAD_REQUEST,msg); }}So we need to override these methods in a custom Servlet!
There is no secret in front of the source code!
Servlet response request phase:
For user requests to reach the Servlet, the Servlet container will create a ServletRequest object and ServletResponse object specific to this request, and then call the Servlet service method. The service method obtains customer request information from the ServletRequest object, processes the request, and returns response information to the customer through the ServletResponse object.
For Tomcat, it will place the passed parameters in a Hashtable, and the definition of the Hashtable is:
The code copy is as follows: private Hashtable<String String[]> paramHashStringArray = new Hashtable<String String[]>();
This is a key-value map of String-->String[].
HashMap thread is not safe, Hashtable thread is safe.
Servlet Termination Phase:
When the WEB application is terminated, or the Servlet container terminates running, or the Servlet container reloads a new instance of the Servlet, the Servlet container will first call the destroy() method of the Servlet, which can free the resources occupied by the Servlet in the destroy() method.
When is a servlet created:
1. By default, when the WEB client requests access to a servlet for the first time, the WEB container will create an instance of the servlet.
2. When a <load-on-startup> child element is specified in the <servlet> element in the web.xml file, the Servlet container will create and initialize the Servlet object in order when starting the web server.
Note: In the web.xml file, some servlets only have <serlvet> elements and do not have <servlet-mapping> elements, so we cannot access these servlets through the URL. This servlet usually configures a <load-on-startup> child element in the <servlet> element, so that the container automatically loads these servlets and calls the init() method when it is started, completing some global initialization work.
When will the web application be started:
1. When the Servlet container is started, all web applications will be started.
2. The controller starts the web application
Comparison between Servlet and JSP:
There are many similarities that can generate dynamic web pages.
The advantage of JSP is that it is good at web page production, and it is more intuitive to generate dynamic pages, but the disadvantage is that it is not easy to track and troubleshoot.
Servlet is a pure Java language and is good at processing processes and business logic. The disadvantage is that it is not intuitive to generate dynamic web pages.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.