Java Servlet API

Source: Internet
Author: User

3. Java Servlet API:

The Java Servlet development tool (JSDK) provides multiple software packages that are required for compiling servlets. It includes two basic software packages for all servlets: javax. Servlet and javax. servlet. http. You can download Java Servlet development tools from sun's Web site. The following describes the http servlet application programming interface provided by javax. Servlet. HTTP.

HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, which is a subclass of GenericServlet that uses a special method to process HTML tables. HTML forms are defined by the <FORM> and </FORM> tags. 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 service () 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. Parameters related to the GET request are added to the URL and sent together with the request. The doGet () method should be used when the data on the server is not modified.
Servlet responses can be of the following types:
An output stream is interpreted by the browser based on its content type (such as text/HTML.
An HTTP Error Response is redirected to another URL, servlet, and JSP.

(3) destroy () method
The destroy () method is executed only once, that is, it is executed when the server is stopped and the Servlet is uninstalled. Typically, the Servlet is disabled as part of the server process. The default destroy () method usually meets the requirements, but can also overwrite it, typically managing server resources. For example, if the Servlet accumulates statistics at runtime, You can compile a destroy () method to save the statistics in the file when the Servlet is not installed. Another example is to close the database connection.
When the server unloads the Servlet, the destroy () method is called after all service () methods are called, or after the specified interval. A Servlet may produce other threads when running the service () method. Therefore, make sure that these threads have been terminated or completed when the destroy () method is called.

(4) GetServletConfig () method
The GetServletConfig () method returns a ServletConfig object, which is used to return initialization parameters and ServletContext. The ServletContext interface provides servlet environment information.

(5) GetServletInfo () method
The GetServletInfo () method is an optional method that provides servlet-related information, such as the author, version, and copyright.
When the server calls the sevlet Service (), doGet (), and doPost () methods, the "request" and "response" objects are required as parameters. The "request" Object provides information about the request, and the "response" Object provides a communication channel for returning the response information to the browser. The related classes in the javax. servlet software package are ServletResponse and ServletRequest, while those in the javax. servlet. http software package are HttpServletRequest and HttpServletResponse.
Servlet communicates with the server through these objects and finally communicates with the client. Servlet can obtain information about the client environment, server environment, and all information provided by the client by calling the "request" object. Servlet can call the method of the "response" object to send a response. The response is prepared to be sent back to the client.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.