Java DRP Project Combat--servlet

Source: Internet
Author: User

Origin

in the commentary Servlet You need to first introduce a word CGI that is, common gatewayinterface is the Universal Gateway interface meaning. It provides a computer program with the HTTP protocol or the WWW service interface, that is, the meaning of the human-computer interface.

The Web app executes the client request on the server. Just started. Is through the CGI program to achieve data transmission on the web, but for every request made by the client , a new instance of the CGI program must be created , which consumes a lot of memory, just to solve the problem, our the Servlet was born.

Servlet is a Java write an application that executes on the server. Processing the requested information and sending it to the client, so its name is Server+applet. For multiple requests from the client,theServlet instance needs to be created only once. Therefore, a large amount of memory is saved, andtheServlet resides in memory after it is initialized. Therefore, there is no need to re-instantiate each request.

Application Architecture

Servlet Container will Servlet dynamically loaded onto the server, HttpServlet interacts with the client using HTTP requests and HTTP responses. So the servlet container supports the request and the HTTP protocol that corresponds to it. What the servlet application architecture sees:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvendrnji2ntqynde3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Indicates that the client's request to the servlet is first received by Httpserver. Httpserver submits the client's HTTP request to the servlet container, and the servlet container invokes the corresponding servlet. The response from the servlet is passed to the servlet container, which in turn transmits the response to the client by Httpserver. Httpserver provides static content and passes all client requests made to the servlet to the servlet container.

In the previous article we learned about Omcat. It is a small, lightweight application server. It is widely used in small and medium-sized systems and concurrent users. Like IIS, Apache, with the ability to process HTML, we also learned at the same time, it is also a servlet and JSP container. For medium. Tomcat is a combination of the two parts of the httpserver and servlet containers.

Life cycle

The life cycle of a servlet is for example:


The servlet executes in a servlet container whose lifecycle is managed by the container. The life cycle of the servlet is represented by the Init (), service (), and Destroy () methods in the Javax.servlet.Servlet interface.

onboarding and instantiation

The servlet container is responsible for loading and instantiating the servlet. This operation is usually run dynamically.

。 When the servlet container is started. Or the servlet instance is created when the container detects that the servlet is needed to respond to the first request.

Below we introduce. How a container dynamically instantiates a servlet. When the servlet container is started. It is necessary to know where the desired servlet class is located, and the servlet container is loaded into the servlet class through the class loader, after it is successfully loaded. Container to create an instance of the servlet. Because the container is using the Java Reflection API to create a servlet instance. Called is the default constructor method of the servlet (that is, a constructor without a reference). So when we write a servlet class, we should not provide a constructor with a parameter.

Initialize

The container initializes the object (configured under the <servlet> tab in Web. xml) by calling the Servlet's init () method.

The purpose of initialization is to have the Servlet object complete some initialization work before processing the client request, such as establishing a connection to the database. Get configuration information, and so on. For each servlet instance. The init () method is called only once.

Request Processing

The servlet container invokes the servlet's service () method to process the request, passing the request and response object as a parameter, and the service () method obtains information about the requested object and processes the request. Visit other resources to get the information you need. The service () method uses the method of the Response object. Returns the response back to the server. Finally arrives at the client.

The service () method may activate other methods to process the request. A new method, such as doget () or dopost () or a program ape himself developed.

Termination of Service

When a container detects that a servlet instance should be removed from the service (typically when the server shuts down), the container invokes the instance's destroy () method. To allow the instance to release the resources it uses. After the Destroy () method call, the container releases the servlet instance, which is then reclaimed by the Java garbage collector.

Assume that the servlet is required to process the request again. The servlet container creates a new servlet instance.

Note

During the life cycle of the servlet, the creation of the servlet instance, the init (), and Destroy () methods of the calling instance are all done only once. When the initialization is complete. The servlet container saves the instance in memory by invoking its service () method. Service for the received request.

Summarize

The use of Servlets will enable us to develop a better system portability, to make our systems more superior, but also effective security issues. About the use of servlets in projects. We will continue to introduce you in the likely article.

Java DRP Project Combat--servlet

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.