[Java Servlet] servlet Introduction

Source: Internet
Author: User

1. What is Servlet?
Servlet is a Java program that uses Java Servlet application design APIs and related classes and methods. In addition to the Java Servlet API, servlet can also use Java software packages used to expand and add to the API. Servlet runs on a Java-enabled web server or application server and extends the capabilities of this server. Java Servlet is like a Java applet to a web browser for a web server. Servlet is loaded into the web server and executed in the web server, while applet is loaded into the web browser and executed in the Web browser. The Java Servlet API defines a standard interface between a Servlet and a Java-enabled server, which makes servlets a cross-server platform feature.
By creating a framework, Servlet extends the server's capabilities to provide web-based request and response services. When the client sends a request to the server, the server can send the request information to the servlet, and let the servlet establish a response from the server to the client. The servlet can be automatically loaded when the web server or client requests the service for the first time. After loading, the servlet continues to run until other clients send a request. Servlet has a wide range of functions. For example, servlet can complete the following functions:
(1) Create and return a complete HTML page containing dynamic content based on the nature of customer requests.
(2) create an HTML page (HTML segment) that can be embedded in an existing HTML page ).
(3) communicate with other server resources (including databases and Java-based applications.
(4) process connections with multiple clients, receive input from multiple clients, and broadcast the results to multiple clients. For example, servlet can be a game server with multiple participants.
(5) When data transmission is allowed in a single connection mode, open a new connection from the server to the applet in the browser and connect the connection
The connection is enabled. When the client and server are allowed to execute sessions in a simple and efficient manner, the applet can also start the connection between the client browser and the server. Communication can be performed through custom protocols or standards (such as IIOP.
(6) use MIME type to filter data for special processing, such as image conversion and server-side (SSI ).
(7) provide customized processing to standard routines for all servers. For example, servlet can modify how to authenticate users.

2. servlet Lifecycle
The servlet lifecycle begins when it is loaded into the memory of the web server and ends when it is terminated or re-loaded.
(1) initialization
Load the servlet at the following time:
If the automatic mount option is configured, it is automatically loaded when the server is started.
When the client sends a request to the servlet for the first time after the server is started
After the servlet is reloaded, the server creates a servlet instance and calls the servlet Init () method. During the initialization phase, the servlet initialization parameters are passed to the servlet configuration object.
(2) Request Processing
For client requests sent to the server, the server creates a request-specific "request" object and a "response" object. The server calls the servlet Service () method, which is used to pass the "request" and "response" objects. The Service () method obtains the request information from the "request" object, processes the request, and uses the "response" Object method to send the response back to the client. The Service () method can call other methods to process requests, such as doget (), dopost (), or other methods.
(3) Termination
When the server no longer needs a servlet or reloads a new servlet instance, the server will call the Servlet's destroy () method.

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" 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 the request information, 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, which 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.