The CGI and Servlet are the programs which reside within the web or application server and assists the communication between the web server and the browser (client side) to generate the web content dynamically. CGI and servlet can be differentiated because they work in different manners and have distinct functionality and features. The CGI (Common Gateway Interface) programs can be designed in the native OS and kept in particular directory. On the other hand, the servlet is a web component which is generally written in Java and run in java virtual machine.
Content: CGI Vs Servlet
|Basis for comparison||CGI||Servlet|
|Basic||Programs are written in the native OS.||Programs employed using Java.|
|Platform dependency||Platform dependent||Does not rely on the platform|
|Creation of process||Each client request creates its own process.||Processes are created depending on the type of the client request.|
|Conversion of the script||Present in the form of executables (native to the server OS).||Compiled to Java Bytecode.|
|Runs on||Separate process||JVM|
|Security||More vulnerable to attacks.||Can resist attacks.|
|Processing of script||Direct||Before running the scripts it is translated and compiled.|
|Portability||Can not be ported||Portable|
Definition of CGI
The CGI (Common Gateway Interface) is an interface which handles external programs (CGI scripts) on a web server to enable the execution of the interactive web pages. CGI was developed by NCSA (National Center for Supercomputing Applications) in 1993. It resides in the server side and enables web browsers to interact with programs on the web server. For example, if a web page queries a database or a user is submitting the form information to the server at that time CGI scripts are invoked. The server passes that information on to an application in two ways GET or POST, then the application responds to the server back to the browser. In this way, browsers get some results for the user.
CGI is a stipulation, for describing a method to run CGI scripts and providing the results back to the server for those particular programs. The function of a CGI is to scan the information received from the browser and produce a proper response, after the completion of the task the CGI script is terminated. In Common Gateway Interface, the common signifies that it can operate fine in any operating system or programming language.
Formerly, UNIX shell scripts and PERL was used to write the CGI programs, that is the reason it named as CGI “scripts”. But, now any of the languages such as C, C++, Perl, Visual Basic or Python can be used. Although, CGI can lead the potential security issues, as the web server can also treat executable files as CGI programs in some specific directories. CGI handle each client request by a separate process which increases the server load making it slower.
Definition of Servlet
A Servlet is a Java-based web component act as an intermediate program which facilitates the interaction between the web browser or HTTP client and HTTP server. Similar to CGI, servlets can also be used to gather the information by the web page forms, show the records from the database and generate dynamic web pages with the help of a container. Servlet is a Java class that does not rely on the platform and further compiled to the bytecode which is platform-independent. The platform-neutral bytecode can be dynamically stored and executed by the Java Web server. Servlet uses socket and RMI mechanism to establish the connection between applets, databases or other database programs.
Servlet container is a part of web server which supports HTTP and HTTPS protocols. It employs the request/response method by using HTTP and HTTPS protocol to enable interaction with web clients. Unlike CGI, servlet runs inside the address space of the web server, where each client is not necessarily treated separately. In the servlet, a collection of constraints are enforced on the server to protect the resources on the server.
Key Differences Between CGI and Servlet
- CGI scripts are written in native OS and stored on the certain directory. On the other hand, servlet programs are usually written in Java which is compiled into Java bytecode and executed in JVM.
- CGI is platform specific which makes it hard to switch between operating systems. As against, Servlets can be executed in any operating system which have installed JVM, hence it is platform-independent.
- In CGI each arriving client request can generate a separate process while servlet, processes are not created needlessly and share the memory space of the JVM.
- CGI scripts are executable programs written in the native OS of the server. In contrast, servlets are compiled to Java bytecode that runs on JVM.
- Servlet is more secure than CGI as it uses Java.
- The speed, performance and efficiency of the servlet is better than CGI.
- CGI scripts can be directly processed. On the contrary, the servlet first translates and compile the program then process it.
- When it comes to portability servlet is portable while CGI is not.
The CGI and Servlet work in the same manner but the use of servlet is advantageous over CGI as the servlet is fast, secure, platform independent, easily developed, accessible through several APIs and supported by various web servers.