Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Difference between Java Servlet and CGI

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

ApproachIt 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 UsedThe 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 OOPsSince 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
PortabilityIt is portable.It is not portable.
PersistenceIt remains in the memory until it is not explicitly destroyed.It is removed from the memory after the completion of the process-basedrequest.
Server IndependentIt can use any of the web-server.It can use the web-server that supports it.
Data SharingData sharing is possible.Data sharing is not possible.
LinkIt links directly to the server.It does not link the web server directly to the server.
HTTP serverIt can read and set HTTP servers.It can neither read nor set HTTP servers.
CostConstruction 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 dependencyIt can be Platform IndependentIt can be Platform dependent.
My Personal Notes arrow_drop_up
Last Updated : 14 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials