Java Drp project practice-Servlet

Source: Internet
Author: User

Java Drp project practice-Servlet
Origin

Before explaining the Servlet, we need to first introduce a word CGI, that is, the Common GatewayInterface is the meaning of a universal gateway interface, which provides an interface for a computer program with the HTTP protocol or WWW Service, that is, the meaning of the human-computer interaction interface. When a Web application runs on a server and a client request is initiated, data is transmitted on the Web through the CGI program. However, for each request made by the client, A new instance of the CGI program must be created, which occupies a large amount of memory. To solve this problem, our Servlet came into being.

Servlet is an application written in java. It runs on the Server and processes the request information and sends it to the client. Therefore, its name is Server + Applet. For multiple client requests, the Servlet instance only needs to be created once, which saves a lot of memory. After initialization, the Servlet will reside in the memory, therefore, no instance is required for each request.

Application Architecture

The Servlet container dynamically loads the Servlet to the server. HTTPServlet uses HTTP requests and HTTP responses to interact with the client. Therefore, the Servlet container supports requests and the corresponding HTTP protocol. Servlet application architecture:


It indicates that the client's Servlet request will be first received by the HTTP server. The HTTP server submits the client's HTTP request to the Servlet container. The Servlet container calls the corresponding Servlet and sends the Servlet response to the Servlet container, the HTTP server then transmits the response to the client. The HTTP server provides static content and transmits requests from all clients to the Servlet container.

In the previous article, we learned about omcat, which is a small lightweight application server. It is widely used in small and medium-sized systems and concurrent users, just like IIS and Apache, it is a Servlet and JSP Container. Tomcat is a combination of HTTP server and Servlet container.

Lifecycle

The Servlet lifecycle is as follows:


Servlet runs in the Servlet container, and its lifecycle is managed by the container. The Servlet lifecycle is represented by the init (), service (), and destroy () methods in the javax. servlet. Servlet interface.

Load and instantiate

The Servlet container is responsible for loading and instantiating Servlet. This operation is generally executed dynamically .. Create a Servlet instance when the Servlet container is started or when the container detects that the Servlet is needed to respond to the first request. Next we will introduce how the container dynamically instantiates the Servlet. When the Servlet container starts, it must know where the required Servlet class is located, the Servlet container loads the Servlet class through the Class Loader. After the Servlet class is loaded successfully, the container creates the Servlet instance. The container creates a Servlet instance through the Java reflection API and calls the default Servlet Constructor (that is, the constructor without parameters). Therefore, when compiling the Servlet class, A constructor with parameters should not be provided.

Initialization

The container will call the Servlet init () method to initialize this object (in web. xml Label ). The purpose of initialization is to allow Servlet objects to complete initialization before processing client requests, such as establishing database connections and obtaining configuration information. For each Servlet instance, the init () method is called only once.

Request Processing

The Servlet container calls the Servlet service () method to process the request, and transmits the request and response object as parameters. The service () method obtains information about the request object, processes the request, and accesses other resources, obtain the required information. The service () method uses the response object method to return the response to the Server and finally reach the client. The service () method may activate other methods to process requests, such as doGet () or doPost (), or a new method developed by the programmer.

Service Termination

When the container detects that a Servlet instance should be removed from the Service (usually when the Server is disabled), the container will call the destroy () method of the instance, this allows the instance to release its resources. After the destroy () method is called, the container releases the Servlet instance and the instance is recycled by the Java garbage collector. If you need this Servlet to process the request again, the Servlet container will create a new Servlet instance.

Note:

During the entire Servlet lifecycle, The init () and destroy () methods for creating the Servlet instance, calling the instance are only performed once. After the initialization is complete, the Servlet container saves the instance in the memory and calls its service () method to serve the received request.

Summary

Using Servlet will enable us to develop a system with better portability, better system performance, and effectively ensure security. Regarding the Use of Servlet in projects, we will continue to introduce it in subsequent articles.

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.