Difference Between Similar Terms and Objects

Difference Between CGI and Servlet

jsp_bookCGI 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.

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

Sharing is caring!

Search DifferenceBetween.net :

Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.


  1. Sir it very good explanation of CGI And Servlet.Thanks……

  2. I almost found everything in this site like exactly what i was in need of, so thanks vr much…

  3. CAN YOU PLEASE EXPLAIN THE LAST POINT. CGI can directly process scripts while it needs to be translated and compiled to before it can be run as a servlet

  4. to the point,clear and informative…

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.

See more about : , ,
Protected by Copyscape Plagiarism Finder