Java servlet basic method introduction

zhaozj2021-02-12  147

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.

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

New Post(0)