What is the difference between Servlet over CGI?

Showing Answers 1 - 12 of 12 Answers

Girish

  • Dec 12th, 2006
 

Servlets are effectively a Java version of CGI scripts, which are written in Perl, C, C++, UNIX shell scripts, etc. There are however, a few important differences.

When a CGI program (or script) is invoked, what typically happens is that a new process is spawned to handle the request. This process is external to that of the webserver and as such, you have the overhead of creating a new process and context switching, etc. If you have many requests for a CGI script, then you can imagine the consequences! Of course, this is a generalization and there are wrappers for CGI that allow them to run in the same process space as the webserver. I think ISAPI is/was one of these.

Java Servlets on the other hand actually run inside the webserver (or Servlet engine). The developer writes the Servlet classes, compiles them and places them somewhere that the server can locate them. 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.

Of course, being Java, the compiled Servlet classes can be moved from one Servlet compatible webserver to another very easily. CGI programs or scripts on the other hand may be platform dependent, need to be recompiled or even webserver dependent.

  Was this answer useful?  Yes

saradhy

  • Dec 21st, 2006
 

The basic concept behind the CGI and Servlets is : CGI is a process based(Heavy weight) and Servlet is a Thread based (Light weight).Insense CGI creates a process for every exceution.So ,this is a time taking process whereas servlet executes by using threadings ,So this is light weight process.Hence Servlets are in more use .Thanks saradhy

  Was this answer useful?  Yes

in cgi , a process is createdfor every client request or call....it increases memory space...

whereas in servlet 1 process is created and then a thread is call for every client request

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions