Table of Content

Servlet Vs CGI



CGI (Commmon Gateway Interface) :

  1. CGI (Common Gateway Interface) is used to provide dynamic content to the user.
  2. CGI is used to execute a program that resides in the server to process data or access databases to produce the relevant dynamic content.
  3. Programs that resides in server can be written in native operating system such as C++.

Diagrammatic Representation :

CGI Web Server
We have listed some problems in CGI technology –

Disadvantages of CGI :

  1. For each request CGI Server receives, It creates new Operating System Process.
  2. If the number of requests from the client increases then more time it will take to respond to the request.
  3. As programs executed by CGI Script are written in the native languages such as C, C++, perl which are platform independent.

Servlet :

CGI programs are used to execute programs written inside the native language. But in Servlet all the programs are compiled into the Java bytecode which is then run in the Java virtual machine.

In Servlet, All the requests coming from the Client are processed with the threads instead of the OS process.

Servlet Vs CGI :

Let’s differentiate Servlet and CGI –

Servlet CGI (Common Gateway Interface)
Servlets are portable CGI is not portable.
In Servlets each request is handled by lightweight Java Thread IN CGI each request is handled by heavy weight OS process
In Servlets, Data sharing is possible In CGI, data sharing is not available.
Servlets can link directly to the Web server CGI cannot directly link to Web server.
Session tracking and caching of previous computations can be performed Session tracking and caching of previous computations cannot be performed
Automatic parsing and decoding of HTML form data can be performed. Automatic parsing and decoding of HTML form data cannot be performed.
Servlets can read and Set HTTP Headers CGI cannot read and Set HTTP Headers
Servlets can handle cookies CGI cannot handle cookies
Servlets can track sessions CGI cannot track sessions
Servlets is inexpensive than CGI CGI is more expensive than Servlets