Servlet API - Servlet Interface

zhaozj2021-02-16  47

A brief description of the servlet interface: the method that defines all Servlets objects. A servlet is a small Java program running on the web server, servlets receives requests from the web client and responds, usually using the HTTP protocol. Implement this interface, you can write a normal servlet extension javax.servlet.GenericServlet class or write an HTTP Servlet extension javax.servlet.http.httpservlet class. This interface defines the method of initializing a servlet, a method of providing a service, and removing a servlet from a server. These methods are regarded as a "life cycle" method. The call sequence of these methods is as follows: 1. When a servlet configuration, call the init () method initialize the servlet 2. When the client issues a request, call the service () method 3. When the servlet service is completed, call the destroy () method, GC (garbage recycling) method and finalized () method

In addition, the changed interface also provides a method that allows the servlet to get startup information - GetServelTConfig, a method that allows servlet to return to its own basic information - GetServletInfo, these basic information such as: authors, version numbers, copyrights, etc.

INIT () method Overview: Public void init (servletconfig config) Throws servletException This method is called by servlet container in servlet, and when the servlet is instantiated, the init () method is immediately evoked, in INIF () Before the method is complete, servlet cannot receive any requests. When the init () method encounters the following two cases, servlet cannot be placed in the service by the servlet container: 1. Method throws a servleTexception exception 2. Method still has not returned after the time defined by the web server (ie method request timeout )

GetServletConfig () Method: Public servletconfig getServletConfig () This method returns a servletconfig object that contains the initialization and startup parameters of the servlet, which is consistent with the parameters transmitted to the INIF () method.

Service () Method Overview: Public Void Service (ServletRequest Req, ServletResponse Res) Throws ServletException, Java.ioException This method is called by servlet container to allow servlet to respond to requests. This method is only called only after the init () method is successfully completed. Servlets run in a multi-threaded mechanism servlet container, thus handling multiple requests at the same time, developers can synchronize access to shared resources, such as files, network connections, servlet classes, instance variables, etc.

GetServletInfo () Method Overview: Public Java.lang.String GetServletInfo () This method returns servlet information, such as: author, version number, copyright, etc. The returned string must be plain text and cannot include any tag language (eg HTML, XML, etc.).

Destroy () Method Overview: Public Void Destroy () is called by servlet container to make sure a servlet has stopped service. This method is only evoked when all threads in Service () methods in servlet have been executed or timeout. Once the method is called, the servlet will no longer call the service () method. This method is used to clean the occupied resources (such as memory, file handles, threads, etc.) while ensuring that the persistent state is synchronized with the current state of the servlet in the memory. Finally, the servlet interface is implemented by the GenericServlet class.

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

New Post(0)