The lifecycle of the servlet and the HTTP protocol.

Source: Internet
Author: User
Tags html form http post

First you need to be clear about the lifecycle of the servlet and the HTTP protocol.
The Serlvet interface defines only one service method that is serviced, and the HttpServlet class implements the method and requires one of the following methods to be invoked:
Doget: Handling Get Requests
DoPost: Processing Post requests
When a client request is made, the service method is invoked and a request and response object is passed. The servlet first determines whether the request is a get operation or a post operation. It then calls one of the following methods: Doget or DoPost. Call the Doget method if the request is get, and call the Dopost method if the request is post. Both Doget and Dopost accept requests (HttpServletRequest) and Responses (HttpServletResponse). Get and post this is the HTTP protocol two methods, plus head, delete, etc.
There is an essential difference between the two methods, get only a stream, the parameter appended to the URL, the number of sizes are strictly limited and can only be strings. Post parameters are passed through another stream, not through the URL, so can be very large, can also pass binary data, such as file upload.
In servlet development, the Get and post methods are processed separately with doget () and Dopost ().
There is also a doservice (), which is a scheduling method that executes the Doservice () first, either get or post, when a request occurs. An angle is implemented in the base class of HttpServlet, which first determines whether get or post is requested, call Doget () if it is, and call Dopost () if it is post. You can also overload the Doservice () method directly so that you can either get or post. Will execute this method.

Service () is defined in the Javax.servlet.Servlet interface, implemented in Javax.servlet.GenericServlet, and Doget/dopost is Javax.servlet.http.HttpServlet, Javax.servlet.http.HttpServlet is a subclass of Javax.servlet.GenericServlet. All that is understood is that all requests are first processed by the service (), and in the Javax.servlet.http.HttpServlet Service () method, the main thing to do is to determine whether the request type is get or Post, and then call the Corresponding to the doget/dopost execution.

Doget: Processing GET requests DoPost: Processing Post requests DoPut: processing a PUT request Dodelete: processing a DELETE request Dohead: processing the head request Dooptions: processing the options request Dotrace: Processing trace requests Typically, developers only need to care about doget and Dopost methods when developing an HTTP-based servlet, and other methods require developers to be very familiar with HTTP programming, so these methods are considered advanced methods. In general, the servlet we implement is extended from HttpServlet. The Doput and Dodelete methods allow developers to support http/1.1 's corresponding features; Dohead is an implemented method that executes doget but only returns the contents of the header to the client that Doget should return to the client; The Dooptions method automatically returns HTTP method information that is directly supported by the servlet, and the Dotrace method returns all header information from the trace request. For containers that support only http/1.0, only the Doget, Dohead, and Dopost methods are used because Http/1service () is defined in the Javax.servlet.Servlet interface. This interface is implemented in Javax.servlet.GenericServlet, and Doget/dopost is implemented in Javax.servlet.http.HttpServlet, Javax.servlet.http.HttpServlet is a subclass of Javax.servlet.GenericServlet. All that is understood is that all requests are first processed by the service (), and in the Javax.servlet.http.HttpServlet Service () method, the main thing to do is to determine whether the request type is get or Post, and then call the The corresponding doget/dopost execution, Doget displays the requested content in the address bar, DoPost hides.
As simple as it is, the Doget method is called in the Dopost method in the servlet, so you can avoid dopost methods when you create a servlet, but the Dopost method is safer when doing large projects involving the transmission of passwords, which is usually no different. Next: The following is mainly about the HTTP servlet application programming interface provided by Javax.servlet.http.

The HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, a subclass of the genericservlet of an HTML table in a specialized way. HTML forms are defined by and 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) Init () method

In the lifetime of the Servlet, only one init () method is executed. 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

The Ervice () 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 invoke the DO function corresponding to the method of the HTTP request. 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 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.

The Doget () method is invoked when a customer issues an HTTP GET request through an 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.

The servlet 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) Destroy () method

The 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.

(4) Getservletconfig () method

The Getservletconfig () method returns a ServletConfig object that is used to return initialization parameters and ServletContext. The ServletContext interface provides environmental information about the servlet.

(5) Getservletinfo () method

The 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, while the related classes in the Javax.servlet.http 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.

<!--Standard Action Servlet Configuration (withdebugging)-->

<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.neusoft.ehr.common.Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>/WEB-INF/classes/property/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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.