Home  >  Article  >  Backend Development  >  What is the essential difference between the running methods of CGI and servlet? What are the essential differences in the principles of web development between PHP and Java?

What is the essential difference between the running methods of CGI and servlet? What are the essential differences in the principles of web development between PHP and Java?

WBOY
WBOYOriginal
2016-12-01 00:01:151882browse

Reply content:

There should be no servers running in CGI mode now. PHP is FastCGI, which is the main reason why this agreement can still be continued today. Servlet can be compared to Python's WSGI. It is a programming interface through which the Web Server and the Web application can be clearly separated. Each incoming request will call the Servlet interface once, so that the Web application can There is no need to care about the technical details such as whether the Web Server uses multi-threading, multi-process, or multiplexing, but only needs to implement this interface. Since it is a programming interface, it must be related to the programming language. Servlet can only be used in Java. Other languages ​​cannot be programmed with Servlet (unless there is a way to be compatible with Java)
FastCGI is a network protocol. It works on sockets and is language-independent. In the past, CGI generally forked a process from the server when each request came in. It only processed this request and exited after the processing was completed. The processing process was to obtain the HTTP header from the environment variable and read the POST from the standard input. Data, output the HTTP response from standard output. Due to the need to constantly create and destroy processes, the performance of this implementation is relatively low, and its functions are also subject to many limitations. FastCGI is an improvement of CGI. It accepts multiple consecutive requests on a single connection and processes them one by one, thus improving throughput. FastCGI, like CGI, has nothing to do with language. Any language can use FastCGI as long as it follows the FastCGI protocol; however, its processing model is limited. Usually, only one request can be processed on a single connection at a time, so a certain degree of concurrency must be achieved. You must create many processes (FastCGI also has the function of multiplexing, but I don’t know how well it is supported)

But in fact, from the perspective of web applications, the difference between the two is not big. Although PHP uses FastCGI, it also processes each request. Call the PHP script once. Although many PHP programmers like to mix web pages and code to make it difficult to maintain, PHP also supports the use of OOP, using MVC like Java; Java also supports templates such as JSP that can embed code. There are some differences. For example, Java works in the same process, can call global objects, can use additional thread pools, etc., while the life cycle of PHP objects is basically limited to the scope of a single request, but many things are still similar. , are all Request-oriented programming. To see the difference, start with simple server code
CGI Look at Python’s built-in CGIHTTPServer
The main code is as follows. In run_cgi, fork the process and then run the cgi program. It has a father-son relationship with the server process
What is the essential difference between the running methods of CGI and servlet? What are the essential differences in the principles of web development between PHP and Java?Look at spring+jetty for Servlet,
What is the essential difference between the running methods of CGI and servlet? What are the essential differences in the principles of web development between PHP and Java?app code and server are in the same process
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn