When writing a servlet, you must directly or indirectly implement the servlet interface. The most possible method is to extend javax. servlet. genericservlet or javax. servlet. HTTP. when javax. servlet. five methods must be implemented for the servlet interface.
Init (servletconfig config)
Service (servletrequest req, servletresponse resp)
Destroy ()
Getservletconfig ()
Getservletinfo ()
The service is the entry point for executing the application logic. The package container calls this method to respond to the incoming request. It can only be called after the servlet is successfully initialized.
In fact, servlet is similar to applet. When a servlet is instantiated, the package container automatically calls a fixed method first, INIT (), and then Service ().....
The genericservlet class provides the basic implementation of the servlet interface, so all its subclasses must implement the service () method.
The httpservlet class extends genericservlet and provides HTTP-specific implementation of servlet interfaces. It is more like a class that all other servlets need to expand.Which defines two forms of service methods:
Service (servirequest req, servletresponse resp)
This method is the implementation of the service () method of genericservlet. It converts the resquest and response objects to httpservletrequest and httpservletresponse respectively, and calls the following overloaded Service () method, therefore, the preceding Service () method should not be reloaded:
Protect void Service (httpservirequest req, httpservletresponse resp)
He uses the HTTP request and response object as the parameter and calls it by the above method. After httpservlet implements this method, it becomes an HTTP request sender who proxies the request to doget (), dopost ()..... wait for doxxxx () method
When the package container receives a request for a servlet, the package container calls the public service method after converting the parameter to httpservirequest and httpservletresponse, this public method calls protects the service according to the HTTP Request Method type, the protected service method calls one of the doxxxx () methods.
The doget () method is called when a GET request is obtained.
The dopost () method is called when a post-type request is obtained.
Httpservlet is inherited from genericservlet, so httpservlet also has the init and destroy life cycle functions and service methods, but httpservlet also has two important dopost and doget methods, use them to Support http post and get methods. That is to say, to support http get methods, we need to overwrite the doget method. To support http post methods, we need to override the dopost method. The main difference between the get method and the post method is:
The data transmission method of the post method is safer, and the transmitted information is not displayed on the web site of the browser, while the get method displays the transmitted information after the web site of the browser.For example, http: // 127.0.0.1: 8080/response. jsp? Name = myself; the POST method is http: // 127.0.0.1: 8080/response. jsp; this difference is more important when it is used for password transmission.
Original article: http://www.cnblogs.com/linux2009/articles/1693573.html