The http servlet application programming interface provided by javax. Servlet. HTTP.
The HTTP Servlet Application Programming Interface uses an HTML table to send and receive data. To create an HTTP Servlet, extend the Http Servlet class, which is a subclass of GenericServlet that uses special methods to process HTML tables. HTML forms are defined by and. A form typically contains input fields (such as text input fields, check boxes, radio buttons, and selection lists) and buttons used to submit data. When the information is submitted, they also specify which Servlet (or other program) the server should execute ). The HttpServlet class contains methods such as init (), destroy (), and service. The init () and destroy () methods are inherited.
(1) init () method
In the Servlet's lifecycle, only the init () method is executed once. It is executed when the server loads the Servlet. You can configure the server to import the Servlet when the server is started or the client accesses the Servlet for the first time. No matter how many clients access the Servlet, init () is not repeated ().
The default init () method is usually compliant with the requirements, but you can also use the custom init () method to overwrite it, typically managing server resources. For example, you may write a custom init () to load GIF images only once, improving the Servlet's performance in returning GIF images and containing multiple client requests. Another example is to initialize the database connection. The default init () method sets the Servlet initialization parameters and uses its ServletConfig object parameters to start the configuration. Therefore, all servlets that overwrite the init () method should call super. init () to ensure that these tasks are still executed. Before calling the service () method, make sure that the init () method has been completed.
(2) service () method
The ervice () method is the core of Servlet. Every time a customer requests an HttpServlet object, the service () method of this object will be called, and a "request" (ServletRequest) object and a "response" (ServletResponse) will be passed to this method) object as a parameter. The service () method already exists in HttpServlet. The default service function is to call the do function corresponding to the HTTP request method. For example, if the HTTP request method is GET, doGet () is called by default (). Servlet should overwrite the do function for the HTTP method supported by Servlet. Because the HttpServlet. service () method checks whether the request method has called an appropriate processing method and does not need to overwrite the service () method. You only need to overwrite the corresponding do method.
When a customer sends an http post request through an HTML form, the doPost () method is called. Parameters related to the POST request are sent to the server as a separate HTTP request from the browser. To modify the data on the server, use the doPost () method.
When a customer sends an http get request through an HTML form or directly requests a URL, the doGet () method is called. The above is the HTTP Servlet Application Programming Interface
- How to listen to Servlet containers
- Introduction to Servlet and JavaServer Page
- Introduction to Http Servlet Request objects
- Servlet API strength
- What is the role of ServletContext?