0% found this document useful (0 votes)
18 views2 pages

Difference Between CGI and Servlet2

CGI creates a new process for each request whereas servlets create a thread for each request. CGI programs run as separate processes with their own address space, which can cause memory overload on the server, whereas servlet threads share the same address space. CGI is not container managed like servlets which are pooled objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Difference Between CGI and Servlet2

CGI creates a new process for each request whereas servlets create a thread for each request. CGI programs run as separate processes with their own address space, which can cause memory overload on the server, whereas servlet threads share the same address space. CGI is not container managed like servlets which are pooled objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

difference between CGI and Servlet

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.

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

what is difference between cgi and servlet in inte...

1.CGI creates a new process for each request Whereas Servlet creates a thread for each request and
services the request in that thread.

2.For each process created by CGI the process is assinged seperate address space.SO there is memory
overload on the server.Whereas for every thread created by the servlet no seperate address space is
created all threads operate in the same parent process address space.so there is no memory overlaod.

3.CGI is not based on pooling Whereas servlet are container managed pooled objects.
CGI vs Servlet

CGI (Common Gateway Interface) is the very first attempt at providing users with dynamic content. It
allows users to execute a program that resides in the server to process data and even access databases
in order to produce the relevant content. Since these are programs, they are written in the native
operating system and then stored in a specific directory. A servlet is an implementation of Java that aims
to provide the same service as CGI does, but instead of programs compiled in the native operating
system, it compiles into the Java bytecode which is then run in the Java virtual machine. Though Java
programs can be compiled into the native code, they still prefer to compile in the Java bytecode.

The first advantage of servlets over CGI is in its platform independence. Servlets can run on any
operating system just as long as a JVM is installed, which means that you would not be having any
problem even if you choose to switch operating systems. With CGI, switching operating system is a
difficult and laborious process as you would need to recompile the programs in the new operating
system.

Since you are running independent programs in CGI, they create their own process when they are
executed, something that does not happen with servlets as they just share in the memory space of the
JVM. This can lead to problems relating to overhead, especially when you increase the number of users
exponentially. It also creates vulnerability issues as the program is not controlled in any way once it is
run on the server.

Later on, the more common method when using CGI is via scripts. This reduces the time needed in
creating programs and are generally more secure. With CGI, you can run scripts right away, while
servlets, you would need to translate the script into Java and compile it into a servlet which adds a little
bit to the loading time.

Summary:
1.CGI are usually executables that are native to the server’s operating system, though servlets can also
be compiled to the native OS it can be compiled to Java bytecode which is then run on a JVM
2.CGI programs are platform dependent while servlets are platform independent
3.CGI programs run as separate processes on the computer while servlets run on the JVM
4.CGI can be more vulnerable to attacks than servlets
5.CGI can directly process scripts while it needs to be translated and compiled to before it can be run as
a servlet

You might also like