Java servlet basic method introduction

xiaoxiao2021-03-06  52

The Java Servlet Development Tool (JSDK) provides multiple packages that need to be used to use these packages when writing servlets. These include two basic packages for all servlets: javax.servlet and javax.servlet.http. Java servlet development tools can be downloaded from Sun's Web Sites. The following mainly introduces the HTTP Servlet application programming interface provided by javax.servlet.http.

HTTP Servlet uses an HTML form to send and receive data. To create an HTTP Servlet, extend the HTTPServlet class, which is a subclass of the genericServlet for the HTML table with a dedicated method. The HTML form is defined by

and tag. The form is typically included in the input field (such as a text input field, checkbox, radio button, and selection list) and buttons for submitting data. When submitting information, they also specify which servlets (or other programs) should be executed. The HTTPSERVLET class contains init (), destroy (), service (), etc. Wherein init () and destroy () methods are inherited.

(1) Init () method In the life period of the servlet, only the init () method is performed. It is performed when the server is loaded with a servlet. You can configure a server to load servlet when you start the server or client to access the servlet. No matter how many client access servlets, INIT () will not be repeated. The default init () method is usually in line with the requirements, but can also be customized to override it, typically manage server-side resources. For example, a custom init () can be written only for one GIF image, improve the performance of servlet to return to GIF images and the performance of multiple client requests. Another example is to initialize the database connection. The default init () method sets the initial parameters of the servlet, and uses its servletconfig object parameters to start the configuration, so all servlets that override the init () method should call super.init () to ensure that these tasks are still executed. Before calling the service () method, you should ensure that the init () method has been completed.

(2) The service () method service () method is the core of the servlet. Whenever a customer requests an HTTPSERVLET object, the service () method of the object is called, and it is passed to this method a "servletRequest) object and a" SERVLETRESPONSE) object as a parameter. The service () method already exists in the HTTPServlet. The default service function is to call the corresponding DO functionality corresponding to the HTTP request. For example, if the HTTP request method is GET, DOGET () is called by default. The servlet should overwrite the DO function for the HTTP method supported by servlet. Because the httpservlet.service () method checks if the request method is called, it is not necessary to override the service () method. Just override the corresponding DO method. = When a customer issues an HTTP POST request via an HTML form, the dopost () method is called. Parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. DOPOST () method should be used when you need to modify the data of the server. = When a customer issues an HTTP GET request or directly requests a URL via an HTML form, the Doget () method is called. The parameters associated with the GET request are added to the URL and send it with this request. You should use the doget () method when the server is not modified. The response of the servlet can be the following types: an output stream, the browser explained based on its content type (such as Text / HTML). An HTTP error response, redirect to another URL, Servlet, JSP. (3) The destroy () method design method is only performed once, ie, execute the method when the server is stopped and unloading the servlet. Typically, servlet is turned off as part of the server process. The default destroy () method is usually in line with requirements, but it can also overwrite it, typically manage server-end resources. For example, if the servlet accumulates statistics at runtime, a DESTROY () method can be written, which is used to save the statistics in the file when the servlet is not loaded. Another example is to turn off the database connection. When the server unresses the servlet, the destroy () method will be called after all service () method calls or after the specified time interval. A servlet may generate other threads when running the service () method, so confirm that when the destroy () method is called, these threads have terminated or completed.

(4) GetServletConfig () method GetServletConfig () method returns a servletconfig object that is used to return to initialization parameters and servletContext. The servletContext interface provides environmental information about servlet.

转载请注明原文地址:https://www.9cbs.com/read-115029.html

New Post(0)