Most of the time rather almost every alternet year this question is going to rock the engineering students who are in Information Technology or Computer science background. I am writing this post for the students who need a quick answare on this.
What is the difference between Servlet and CGI ?
CGI is very initial attempt to work with client server architecture.A CGI creates a new process every-time it receives a new request from client.This can be written in various language.Basically a heavy weight process
|Servlet is more advanced for client server architecture As it is proved by java,it process a new thread upon getting new request. It is lightweight process|
|Number of process for any server is predefined,hence multiple request if , went to the server and server could not process it , there may be a loss in process via request||Thread is a light weight process it is true but there is no limit for that,hence there is no loss in process.|
|CGI executes the processes in their own OS Shell, doesn’t have concurrency conflicts. but creation of OS shell is a heavy weight process.It takes a higher time to be created. Process life cycle takes a lot of time to complete and it effects response time of the server (goes high.)||The first time a Servlet is requested, it is loaded into memory and cached. From then on, the same Servlet instance is used, with different requests being handled by different threads. Thread lifecycle is very short hence improve the server response.|
|CGI is mainly for text processing application where less or no security or object orientation process required.||Servlet is for Enterprise application where mostly we use object orientation|
|CGI servers requires to start a new interpreter for every request which eats up the resources and probable process||Upon request a new thread is opened so no need to open interpreter.|
|There might be a chance Client submits a request to CGI that terminates before responding.The browser will be still in waiting for information mode.
||Not possible in Servlet|
|Any bug can not crash the web server||Any bug can is easily caught by exception handler.|
|Powered by Java.Hence highly scalable and servlet programming is platform independent as long as the JVM is running and supported by OS.|
|No separation between presentation and business layer.CGI cannot read and set HTTP headers, handle cookies, tracking sessions.||Business layer and Presentation layer is always different as Servlet can read and set HTTP headers, handle cookies, tracking sessions.|
|CGI can process only one request||Servlet can process multiple request.|
|CGI is not based on pooling (which hold no external resource but occupy memory i.e. not destroy after used) Its container managed pooling object.||JVM supports pooling of objects so as Servlet.|
|CGI creates its own memory space for every request viz process.||Servlet shares the memory space.|
|Advantage of CGI is that it can precess and run any script any given point of time.||In servlet any script first get translated into java then it gets compiled and run.|