Glossary: CGI (Common Gateway Interface) and servlet

CGI (Common Gateway Interface), Common Gateway Interface Common Gateway Interface
, CGI for short, is a technology that dynamically generates response content according to request information. Through CGI, the Web server can start different external programs according to different requests, and forward the content of the request to the program. After the program execution ends, the execution result is returned to the client as a response. That is to say, for each request, a new process must be generated for processing. Because each process occupies a lot of server resources and time, the server cannot handle many concurrent requests at the same time. In addition, CGI programs are all related to the operating system platform. Although CGI made a great contribution to the development of Internet applications in the early days of the Internet outbreak, it began to decline gradually with the development of technology.

Servlet
Servlet was originally proposed by James Gosling in 1995. Because the use of this technology requires complex Web server support, it was not paid attention to at that time and was abandoned. Later, with the increase in the complexity of Web applications and the requirement to provide higher concurrent processing capabilities, Servlets were picked up again and implemented on the Java platform. Now Servlets are referred to as Java Servlets. Java Servlet requires that it must run in the Web server, and it belongs to the division of labor and complementary relationship with the Web server. To be precise, when actually running, the Java Servlet and the Web server will be integrated, running in the same Java Virtual Machine (JVM) as a program. Unlike CGI, servlets start a thread for each request, not a process. This processing method greatly reduces the number of processes in the system and improves the concurrent processing capability of the system. In addition, because the Java Servlet runs on the virtual machine, it also solves the cross-platform problem. Without the emergence of Servlet, there would be no Internet today.
After the emergence of Servlet, with the expansion of the scope of use, people found a big drawback of it. That is, in order to be able to output HTML format content, it is necessary to write a lot of repetitive code, resulting in unnecessary duplication of work. In order to solve this problem, JavaServet Pages technology, that is, JSP, is produced based on Servlet technology. Servlet and JSP work in division of labor. Servlet focuses on solving computing and business logic problems, while JSP focuses on solving display problems. Servlet and JSP together have brought great contributions to the development of Web applications. Many Java Web application development frameworks that appeared later are based on these two technologies, more precisely, are based on Servlet technology.

Compared with
the CGI (Common Gateway Interface) program, it is mainly written in Perl, Shell Script or C, which can provide dynamic content to the client, that is, each time the client browser accesses a page, it can see different content. CGI application development is difficult because it requires the programmer to have knowledge of handling parameter passing, which is not a universal skill. CGI is not portable, and CGI applications written for a particular platform can only run in that environment. Each CGI application exists in a process that is activated by a client request, and is uninstalled after the request has been serviced. This mode will incur high memory, CPU overhead, and cannot serve multiple clients in the same process.
The main advantage of servlets over CGI is that a servlet is activated by the first request sent by the client, and then it continues to run in the background, waiting for subsequent requests. Each request will spawn a new thread instead of a full process. Multiple clients can be served simultaneously in the same process. Generally speaking, the servlet process is only unloaded when the Web Server is unloaded.

Servlets provide all the advantages of Java applications - portable, robust, and easy to develop. Using Servlet Tag technology, servlets can generate dynamic content embedded in static HTML pages.
The main advantage of servlets over CGI is that a servlet is activated by the first request sent by the client, and then it continues to run in the background, waiting for subsequent requests. Each request will spawn a new thread instead of a full process. Multiple clients can be served simultaneously in the same process. Generally speaking, the servlet process is only unloaded when the Web Server is unloaded.

Servlet is in the server process, it runs its service method through multi-threading, one instance can serve multiple requests, and its instance is generally not destroyed.
However, CGI generates a new process for each request, and it is destroyed after the service is completed, so the efficiency is lower than that of Servlet.


Reprinted from: http://blog.csdn.net/yttcjj/article/details/6975907

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326816774&siteId=291194637