The Java Servlet Development Kit (JSDK) provides multiple software packages, which are needed when writing Servlets. These include two basic packages for all servlets: javax.Servlet and javax.Servlet.http. The Java Servlet development tools can be downloaded from Sun's website.
Recommended course:Java Tutorial.
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 GenericServlet with specialized methods for handling HTML tables. HTML forms are defined by and tags. Forms typically contain 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 execute. The HttpServlet class includes init(), destroy(), service() and other methods. The init() and destroy() methods are inherited.
Common methods of Servlet API:
(1)Init() method of Servlet API
In the life cycle of Servlet, the init() method is only executed once. It is executed when the server loads the servlet. You can configure the server to load a servlet when it is started or when a client accesses the servlet for the first time. No matter how many clients access the servlet, init() is never executed repeatedly.
The default init() method usually meets the requirements, but it can also be overridden with a custom init() method, typically to manage server-side resources. For example, you might write a custom init() to load a GIF image only once, improving the performance of a Servlet returning a GIF image and containing multiple client requests. Another example is initializing a database connection. The default init() method sets the Servlet's initialization parameters 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 performed. Before calling the service() method, you should ensure that the init() method has been completed.
(2) Service() method of Servlet API
The service() method is the core of Servlet. Whenever a client requests an HttpServlet object, the object's service() method is called, and a "request" (ServletRequest) object and a "response" (ServletResponse) object are passed to this method as parameters. The service() method already exists in HttpServlet. The default service function is to call the do function corresponding to the method of the HTTP request. For example, if the HTTP request method is GET, doGet() is called by default. Servlets should override do functionality for HTTP methods supported by the servlet. Because the HttpServlet.service() method checks whether the request method calls the appropriate handler, there is no need to override the service() method. Just override the corresponding do method and you're done.
◆When a client makes an HTTP POST request through 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. When you need to modify server-side data, you should use the doPost() method.
◆When a client issues an HTTP GET request through an HTML form or directly requests a URL, the doGet() method is called. Parameters related to the GET request are added to the end of the URL and sent with this request. The doGet() method should be used when the server-side data will not be modified.
Servlet's response can be of the following types:
◆An output stream, which the browser interprets according to its content type (such as text/HTML).
◆An HTTP error response, redirect to another URL, Servlet, JSP.
(3)Destroy() method of Servlet API
The destroy() method is only executed once, that is, it is executed when the server stops and the Servlet is uninstalled. Typically, the servlet is shut down as part of the server process. The default destroy() method is usually adequate, but it can be overridden, typically to manage server-side resources. For example, if your servlet accumulates statistics as it runs, you can write a destroy() method that saves the statistics in a file when the servlet is not loaded. Another example is closing a database connection.
When the server uninstalls the Servlet, the destroy() method will be called after all service() method calls are completed, or after the specified time interval has passed. A Servlet may generate other threads when running the service() method, so please confirm that these threads have been terminated or completed when the destroy() method is called.
(4)GetServletConfig() method of Servlet API
GetServletConfig() method returns a ServletConfig object, which is used to return initialization parameters and ServletContext. The ServletContext interface provides environmental information about the Servlet.
(5)GetServletInfo() method of Servlet API
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 the sevlet, 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 for returning response information to the browser. The relevant classes in the javax.Servlet package are ServletResponse and ServletRequest, while the relevant classes in the javax.Servlet.http package are HttpServletRequest and HttpServletResponse. Servlets communicate with the server and ultimately the client through these objects. Servlet can learn the client environment, server environment information and all information provided by the client by calling the method of the "request" object. A Servlet can call methods on the Response object to send a response that is ready to be sent back to the client.
The above is the detailed content of What does servlet api mean?. For more information, please follow other related articles on the PHP Chinese website!