Differences between servlet, genericservlet, and httpservlet

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.