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:
|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.
- Difference between ServletConfig and ServletContext in Java Servlet
- Difference between Servlet and JSP
- Servlet Collaboration In Java Using RequestDispatcher and HttpServletResponse
- Java Servlet and JDBC Example | Insert data in MySQL
- Javax.servlet.http.Cookie class in Java
- URL Rewriting using Java Servlet
- Hidden Form Field using Annotation | Java Servlet
- How to add Image to MySql database using Servlet and JDBC
- Starting with first Servlet Application
- The HttpSession Interface in Servlet
- Life Cycle of a Servlet
- Pattern Printing question asked in CGI Coding Round
- CGI Interview Experience | (Campus placement)
- CGI Interview Experience (August, 2018)
- CGI Recruitment Process (On-Campus 2018)
- CGI Interview Experience (On Campus 2018)
- CGI Interview Experience (On Campus for Associate Software Engineer)
- CGI Interview Experience - ECE
- CGI Interview experience
- Difference between Core Java and Advanced Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.