Open In App

Difference between Java Servlet and CGI

Improve
Improve
Like Article
Like
Save
Share
Report

The world has changed into 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 web-based applications namely servlets and CGI. 

Servlet is a Java class that 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 web 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 thread-based. 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 the process-basedrequest.
Server Independent 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 link the web server 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 threads are not costly.  Construction and destruction of the new processes are costly. 
Speed  Its can speed is slower. It can speed is faster.
Platform dependency It can be Platform Independent It can be Platform dependent.

Last Updated : 14 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads