Several methods of the servlet (init (), service (), Doget (), DoPost (), Destroy (), Getservletconfig (), Getservletinfo ())

Source: Internet
Author: User
Tags html form http post
httpservlet  uses an HTML form to send and receive data. To create a httpservlet, expand the   httpservlet  class, which is a subclass of the genericservlet of html  forms in a specialized way. HTML forms are defined by <form> and </form> tags. A form typically contains input fields, such as text input fields, check boxes, radio buttons, and select lists, and buttons for submitting data. When submitting information, they also specify which servlet (or other program) the server should perform. The HttpServlet class contains methods such as Init (), Destroy (), service (), and so on. where the Init () and Destroy () methods are inherited. (1) The Init () method executes only one init () method in the lifetime of the Servlet. It is executed when the server is mounted in the Servlet. You can configure the server to enter the servlet into the servlet for the first time on the boot server or client. Regardless of how many clients access the Servlet, init () is not repeated. The default init () method is usually compliant, but it can also be overridden with a custom init () method, typically managing server-side resources. For example, you might write a custom init () to only mount a GIF image at a time, and improve the performance of the Servlet returning a GIF image and a request that contains multiple clients. Another example is the initialization of a database connection. The default init () method sets the servlet initialization parameters and starts the configuration with its ServletConfig object arguments, so all Servlet overrides of the Init () method should call Super.init () to ensure that these tasks are still performed. Before calling the service () method, make sure that the init () method is complete. (2) Service () method service () method is the core of the Servlet. Whenever a client requests a HttpServlet object, the service () method of the object is invoked and passed to the method a request (ServletRequest) object and a response (Servletresponse) object as an argument. The service () method already exists in HttpServlet. The default service function is to call the HTThe method of the TP request corresponds to the DO function. For example, if the HTTP request method is get, the doget () is invoked by default. The servlet should overwrite do functionality for the HTTP methods supported by the servlet. Because the Httpservlet.service () method checks whether the request method calls the appropriate processing method, you do not have to overwrite the service () method. Just overwrite the appropriate do method. The Servlet's response can be of the following types: an output stream that the browser interprets based on its content type, such as text/html. An HTTP error response, redirected to another URL, servlet, JSP. (3) Doget () method The Doget () method is invoked when a customer issues an HTTP GET request through a html  form or requests a URL directly. The parameters associated with the GET request are added to the URL and sent along with the request. The Doget () method should be used when server-side data is not modified. (4) DoPost () method The DoPost () method is invoked when a customer issues an HTTP POST request through an HTML form. The parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. You should use the DoPost () method when you need to modify server-side data. (5) The Destroy () method Destroy () method executes only once, that is, when the server is stopped and the Servlet is uninstalled. Typically, the Servlet is closed as part of the server process. The default Destroy () method is usually compliant, but it can also be overridden, typically managing server-side resources. For example, if the servlet accumulates statistics at run time, you can write a destroy () method that is used to save statistics in a file when the servlet is not mounted. Another example is to close the database connection. When the server uninstalls the Servlet, the Destroy () method is invoked after all service () method calls have been completed, or after a specified interval. A Servlet may produce additional threads when it runs the service () method, so make sure that when the destroy () method is invoked, the threads are terminated or completed. (6) Getservletconfig () method Getservletconfig () method returns a ServletcoNfig object that is used to return initialization parameters and ServletContext. The ServletContext interface provides environmental information about the servlet. (7) The Getservletinfo () method Getservletinfo () method is an optional method that provides information about the servlet, such as author, version, and copyright. The request and response objects are required as parameters when the server invokes the Sevlet service (), Doget (), and DoPost () three methods. The Request object provides information about the request, and the response object provides a means of communication that returns the response information to the browser. The related classes in the Javax.servlet package are servletresponse and ServletRequest, and Javax.servlet. The related classes in the http  software package are httpservletrequest and HttpServletResponse. The Servlet communicates with the server through these objects and eventually communicates with the client. The Servlet can learn about the client environment, the server environment, and all the information provided by the client by invoking the method of the Request object. The Servlet can call the response object's method to send a response that is ready to be sent back to the client.

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.