In the Servlet GenericServlet class, why are there two init () Methods: genericservletinit
To understand this, you must first understand the Servlet container call process. The call process is as follows:
Access the Servlet for the first time
1. Call init (ServletConfig config) for initialization. ServletConfig encapsulates the web. the <init-param> Configuration initialization parameter in xml, which is created by the Servlet container and passed to the current serlvet
2. Call the service (ServletRequest req, ServletResponse res) method to process customer requests.
3. Call the destroy () method to destroy the Servlet instance. Of course, this is only to illustrate the complete process. In fact, the destroy () method is not destroyed after the servlet is called.
Access the Servlet later
1. Call the service (ServletRequest req, ServletResponse res) method to process customer requests.
2. Call the destroy () method to destroy the Servlet instance. Of course, this is only to illustrate the complete process. In fact, the destroy () method is not destroyed after the servlet is called.
From the above call process, the Serlvet container only cares about the init method init (ServletConfig config), service (ServletRequest req, ServletResponse res), and destroy () with parameters (), other additional methods are additional products, which means that the init method without parameters is not necessary. Why does this method have been defined in the GenericServlet abstract class?
Let's take a look at the implementation of the two init methods in the GenericServlet class.
Public void init (ServletConfig config) throws ServletException {
This. config = config;
This. init ();
}
Public void init () throws ServletException {
} From the above implementation, init (ServletConfig config) will call the init method without parameters to initialize the Serlvet instance, but the init method without parameters will do nothing.
If the GenericServlet class only implements the init (ServletConfig config) method, the code should be as follows:
Public void init (ServletConfig config) throws ServletException {
This. config = config;
} If a subclass inherits the GenericServlet class that defines only one init method, and you want to add some initialization parameters during initialization, you must override the init (ServletConfig config) of the GenericServlet class) the method is as follows:
Public class Test extends GenericServlet {
Public int a = 0;
Public String B;
Public void init (ServletConfig config) throws ServletException {
Super. init (config); // or this. config = config;
This. a = XXX;
This. B = XXX;
}
Public void
Service(ServletRequest req, ServletResponse res) throws ServletException, java. io. IOException {
....
}
} The above implementation class is okay. We can use the getServletConfig () method to get the ServletConfig object for use normally.
However, if you forget to call super during the development process. init (config); // or this. config = config; this statement causes a big problem. getServletConfig () returns null and cannot use ServletConfig. Therefore, GenericServlet defines a non-parameter init () method, and the subclass only needs to inherit this method. This prevents similar problems.
From Weizhi note (Wiz)
Servlet init Problems
Unrestricted code exception
What is the difference between the servce method in the HttpServlet class and the servce In the Servlet?
Servlet is a server-side Java application independent of platform and protocol. It can generate dynamic Web pages. It serves as the intermediate layer between the request sent by a Web browser or other HTTP client programs and databases or applications on the HTTP server.
Servlet is an interface. Its service method is called whenever a user sends a request. But there is no specific implementation in the interface.
HttpServlet is a specific implementation of Servlet. HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, which is a subclass of GenericServlet that uses a special method to process HTML tables. HTML forms are defined by the <FORM> and </FORM> tags. A form typically contains input fields (such as text input fields, check boxes, radio buttons, and selection lists) and buttons used to submit data.
Every time a customer requests an HttpServlet object, the service () method of this object will be called, and a "request" (ServletRequest) object and a "response" (ServletResponse) will be passed to this method) object as a parameter. The service () method already exists in HttpServlet. The default service function is to call the do function corresponding to the HTTP request method. For example, if the HTTP request method is GET, doGet () is called by default (). Servlet should overwrite the do function for the HTTP method supported by Servlet. Because the HttpServlet. service () method checks whether the request method has called an appropriate processing method and does not need to overwrite the service () method. You only need to overwrite the corresponding do method.