Difference between Java Servlet and CGI

The world has changed to a mobile-first era but even today, none of the applications could emerge as effective as the web-based apps. Surfacing on top of this is the prevalence of progressive web apps that perform functions identical to mobile apps. In this article, we will understand the difference between the two functionalities in the web-based applications namely servlets and CGI.

Servlet: A servlet is a Java class which is used to extend the capabilities of servers that host applications accessed by means of a request-response model. Servlets are mainly used to extend the applications hosted by webs servers, however, they can respond to other types of requests too. For such applications, HTTP-specific servlet classes are defined by Java Servlet technology. All the programs of Servlets are written in JAVA and they get to run on JAVA Virtual Machine. The following image describes how a request from clients is served with the help of threads:

Common Gateway Interface(CGI): The Common Gateway Interface (CGI) provides the middleware between WWW servers and external databases and information sources. The World Wide Web Consortium (W3C) defined the Common Gateway Interface (CGI) and also defined how a program interacts with a HyperText Transfer Protocol (HTTP) server. The Web server typically passes the form information to a small application program that processes the data and may send back a confirmation message. This process or convention for passing data back and forth between the server and the application is called the common gateway interface (CGI). The following image describes how a web server acts as an intermediate between the CGI program and the client browser.

The following table explains the difference between the servlet and CGI:

Basis Servlet CGI
Approach It is thread based i.e. for every new request new thread is created. It is process based i.e. for every new request new process is created.
Language Used The codes are written in JAVA programming language. The codes are written any programming language.
Object Oriented Since codes are written in Java, it is object oriented and the user will get the benefits of OOPs Since codes are written in any language, all the languages are not object oriented. So, the user will not get the benefits of OOPs
Portability It is portable. It is not portable.
Persistence It remains in the memory until it is not explicitly destroyed. It is removed from the memory after the completion of request.
Server Indepent It can use any of the web-server. It can use the web-server that supports it.
Data Sharing Data sharing is possible. Data sharing is not possible.
Link It links directly to the server. It does not links directly to the server.
HTTP server It can read and set HTTP servers. It can neither read nor set HTTP servers.
Cost Construction and destruction of new thread  is not costly.  Construction and destruction of new process is costly. 

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.