What are the advantages of using Servlets rather than CGIs?

Mikael Jakobsson

When a normal CGI script (or program) is executed, it is spawned in a separate process by the web server, and the output of that process becomes the resulting page that is sent back to the client. Each request to the web server will result in a new process beeing started. Starting a process will create some overhead, in performance and in memory usage.

In servlets however, only one process is spawned, and that process contains the JVM. Each request will cause the JVM to create a new thread that will be responsible for creating the output that will be the result. Creating a thread requires much less resources to start up, both CPU and memory.

This approach can make servlets much more efficient than even an optimized C program acting as a CGI, simply because of the much reduced overhead.

However, there are upcoming standards around CGI that use the same approach with threads instead of processes, such as the Perl FastCGI module. So the above holds true for the comparision with a 'normal' CGI, not necessarily with all variants.

There are of course other advantages.

  • Java is a well structured language suitable for larger systems. Many of the script languages used for CGI are formidable for small scripts and applications, but when larger systems are built, it is not uncommon to find that the code is a horror to maintain.
  • It is probably easier to find a capable Java programmer than a Perl/CSH/PHP3/... programmer. This is a point not to be forgotten if you want someone else to be able to take over and/or understand your code.
Undoubtedly there are other advantages, but I stop here. I just want to add that:

Using the correct technology is not as important as using the technology correctly.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact