The difference between Servlets, Genericservlet, and HttpServlet (RPM)

Source: Internet
Author: User

When writing a servlet, the servlet interface must be implemented directly or indirectly, The most likely way to achieve this is to extend the Javax.servlet.genericservlet or Javax.servlet.http.httpservlet when implementing the Javax.servlet.servlet interface, you must implement 5 methods

Init (servletconfig config) service (servletrequest Req,servletresponse resp) destroy () Getservletconfig () Getservletinfo ()

The service is the entry point for executing the application logic, and the package container calls this method in response to the request, but only after the servlet has been successfully initialized can the servlet and the applet be similar, when a servlet is instantiated, The package container automatically calls the fixed method first init (), then the service () .....

The Genericservlet class provides a basic implementation of the Servlet interface, so his subclasses must implement the service () method The HttpServlet class extends the Genericservlet and provides the HTTP-specific implementation of the Servlet interface, which is more like a class that all other Servlets extend , in which he defines two forms of service methods:

   Service (servirequest req,servletresponse resp)      This method is an implementation of the service () method of Genericservlet , he translates the Resquest,response objects into HttpServletRequest and HttpServletResponse and invokes the service () method as overloaded below, so the service () method above should not be overloaded:  

   protect void Service (Httpservirequest req,httpservletresponse resp)      request for HTTP , response the object as a parameter and is called by the method above, HttpServlet implements this method to become the distributor of an HTTP request, and he proxies the request to Doget (), Dopost (). Doxxxx () method       When the containment device receives a request for a servlet, the package container calls the public service method to convert the parameter to Httpservirequest. After HttpServletResponse, this public method invokes a protected service based on the type of HTTP request method, and the protected service method calls one of the doxxxx () methods .    The Doget () method is called when a Get type request is obtained   the Dopost () method is called when a request for a post type is received   HttpServlet is inherited from Genericservlet. So HttpServlet also has both the Init and destroy life cycle functions as well as the service method, but HttpServlet also has two important dopost methods and Doget methods, and uses them to support the HTTP post and get methods, That is, if you want to support the Get method of HTTP, you should overwrite the Doget method, if you want to support the HTTP POST method, you should overwrite the Dopost method. The main difference between the Get method and the Post method is:     the post method is more secure in the way data is passed, does not display the information passed to the browser's URL, and the Get method displays the information passed in the browser's URL , such as the browser URL with the Get method: Http://127.0.0.1:8080/response.jsp?name=myself; and the Post method is: http://127.0.0.1:8080/ Response.jsp: This distinction is more important when used in the transmission of passwords.

Original: http://www.cnblogs.com/linux2009/articles/1693573.html

The difference between Servlets, Genericservlet, and HttpServlet (RPM)

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.