Introduction to Basic methods in Java Servlet API class Library

Source: Internet
Author: User
Tags contains html form http post http request client
Servlet

The Java Servlet development tool (JSDK) provides a number of packages that need to be used when writing a servlet. These include two basic packages for all Servlet: Javax.servlet and Javax.servlet.http. Java Servlet Development tools can be downloaded from Sun Company's web site. The following describes the HTTP servlet application programming interfaces provided by Javax.servlet.http.

The HTTP Servlet uses an HTML table to send and receive data. To create an HTTP Servlet, extend the HttpServlet class, a subclass of the genericservlet of an HTML table in a specialized way. 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 select lists, and buttons for submitting data. When submitting information, they also specify which servlet (or other program) the server should perform. The HttpServlet class contains methods such as Init (), Destroy (), service (), and so on. where the Init () and Destroy () methods are inherited.

init () method

In the lifetime of the Servlet, only one init () method is executed. It is executed when the server is mounted in the Servlet. You can configure the server to enter the servlet into the servlet for the first time on the boot server or client. Regardless of how many clients access the Servlet, init () is not repeated.

The default init () method is usually compliant, but it can also be overridden with a custom init () method, typically managing server-side resources. For example, you might write a custom init () to only mount a GIF image at a time, and improve the performance of the Servlet returning a GIF image and a request that contains multiple clients. Another example is the initialization of a database connection. The default init () method sets the servlet initialization parameters and starts the configuration with its ServletConfig object arguments, so all Servlet overrides of the Init () method should call Super.init () to ensure that these tasks are still performed. Before calling the service () method, make sure that the init () method is complete.

Service () method

The service () method is the core of the Servlet. Whenever a client requests a HttpServlet object, the service () method of the object is invoked and passed to the method a request (ServletRequest) object and a response (Servletresponse) object as an argument. The service () method already exists in HttpServlet. The default service function is to invoke the DO function corresponding to the method of the HTTP request. For example, if the HTTP request method is get, the doget () is invoked by default. The servlet should overwrite do functionality for the HTTP methods supported by the servlet. Because the Httpservlet.service () method checks whether the request method calls the appropriate processing method, you do not have to overwrite the service () method. Just overwrite the appropriate do method.

= The DoPost () method is invoked when a client issues an HTTP POST request through an HTML form. The parameters associated with the POST request are sent from the browser to the server as a separate HTTP request. You should use the Dopost () method when you need to modify server-side data.

= The Doget () method is invoked when a client issues an HTTP GET request through an HTML form or requests a URL directly. The parameters associated with the GET request are added to the URL and sent along with the request. The Doget () method should be used when server-side data is not modified.

The servlet response can be of the following types:

An output stream that the browser interprets based on its content type (such as text/html).

An HTTP error response, redirected to another URL, servlet, JSP.

Destroy () method

The Destroy () method executes only once, that is, when the server is stopped and the servlet is uninstalled. Typically, the Servlet is closed as part of the server process. The default Destroy () method is usually compliant, but it can also be overridden, typically managing server-side resources. For example, if the servlet accumulates statistics at run time, you can write a destroy () method that is used to save statistics in a file when the servlet is not mounted. Another example is to close the database connection.

When the server uninstalls the Servlet, the Destroy () method is invoked after all service () method calls have been completed, or after a specified interval. A servlet may produce additional threads when it runs the service () method, so make sure that when the destroy () method is invoked, the threads are terminated or completed.

getservletconfig () method

The Getservletconfig () method returns a ServletConfig object that is used to return initialization parameters and ServletContext. The ServletContext interface provides environmental information about the servlet.

Getservletinfo () method

The Getservletinfo () method is an optional method that provides information about the servlet, such as author, version, and copyright.

The request and response objects are required as parameters when the server invokes the Sevlet service (), Doget (), and Dopost () three methods. The Request object provides information about the request, and the response object provides a means of communication that returns the response information to the browser. The related classes in the Javax.servlet package are servletresponse and ServletRequest, while the related classes in the Javax.servlet.http package are httpservletrequest and HttpServletResponse.

The Servlet communicates with the server through these objects and eventually communicates with the client. The Servlet can learn about the client environment, the server environment, and all the information provided by the client by invoking the method of the Request object. The Servlet can call the response object's method to send a response that is ready to be sent back to the client.



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.