HTTP Servlet inherits the Gencenservlet class Gencenservlet implements two interfaces • • One for the ServletConfig Setup interface and one for the servlet interface only if(1) Init () method controlThe life cycle of a servlet ·
8 Methods of heart memory
HTTP
The Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class,
This class is a subclass of Genericservlet that uses specialized methods to process HTML tables. HTML forms are made up of <FORM> and
</FORM>
tags are defined. The form typically contains input fields such as text input fields, check boxes, radio buttons, and select lists, and buttons for submitting data. When information is submitted, they also specify which of the servers should perform
A servlet (or other program). The HttpServlet class contains methods such as Init (), Destroy (), service (), and so on. which
The Init () and Destroy () methods areInheritanceOf
(1) Init () method
The init () method is executed only once during the lifetime of the Servlet. It is loaded into the Servlet on the server
When executed. You can configure the server to get the servlet into the servlet at the start of the server or client for the first time. Regardless of how many client access
Servlets do not repeat init ().
The default init () method is usually compliant, but can also be customized with the Init ()
method to overwrite it, typically managing server-side resources. For example, you might write a custom init () to use only one time to mount a GIF image, to improve the Servlet return GIF
Image and the performance that contains multiple client requests. Another example is the initialization of a database connection
Pick up. The default init () method sets the initialization parameters of the Servlet and uses its ServletConfig object parameter to start the configuration, so all overrides
The Servlet of the Init () method should call Super.init () to ensure that these tasks are still performed. When the service () is called
method, you should ensure that you have completed the Init () method.
(2) Service () method
The service () method is the core of the Servlet. Whenever a customer requests a HttpServlet object, the service () of the object
The method is called and passed to the method a "request" (ServletRequest) object and a "response" (Servletresponse) object as a parameter.
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, and doget () is called by default. Servlets should overwrite do for servlet-supported HTTP methods
Function. Because the Httpservlet.service () method checks whether the request method invokes the appropriate processing method, it does not have to overwrite the service ()
Method. Just overwrite the appropriate do method.
The response of a servlet can be of the following types:
An output stream, which is interpreted by the browser 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 called when a client 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 with the request. The Doget () method should be used when the server-side data is not modified.
(4) DoPost () method
The DoPost () method is called when a client 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 the server-side data.
(5) Destroy () method
The Destroy () method executes only once, which is executed when the server is stopped and the servlet is dismounted. Typically, the Servlet
Shut down 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 when the servlet is not mounted
The statistics are saved in the file. Another example is closing the database connection.
When the server uninstalls the Servlet, it will be in all service ()
The Destroy () method is called after the method call finishes, or after the specified time interval. A servlet running the service ()
method may produce additional threads, make sure that the threads are terminated or completed when the Destroy () method is called.
(6) Getservletconfig () method
The Getservletconfig () method returns an ServletConfig object that is used to return initialization parameters and ServletContext. The ServletContext interface provides environment information about the servlet.
(7) Getservletinfo () method
The Getservletinfo () method is an optional method that provides information about the servlet, such as author, version, and copyright.
When the server calls the three methods of Service (), Doget (), and Dopost () of Sevlet, both the request and response objects are required as parameters. The Request object provides information about the request, and the response object provides a communication path to return the response information to the browser.
Javax.servlet
The related classes in the package are Servletresponse and ServletRequest, and Javax.servlet.http
The related classes in the package are HttpServletRequest and HttpServletResponse. Servlet
These objects communicate with the server and eventually communicate with the client. Servlet
Information about the client environment, the server environment, and all information provided by the client can be learned by invoking the method of the Request object. Servlet
You can call the response object's method to send the response, which is ready to be sent back to the client.
Several important methods of HTTP Servlet