UK Sales: 0800 035 6364 | | We'll Call You

Servlets vs. CGI scripts

As most developers realize, the advantages of CGI technology are many. Not only has it been around forever and is tried and true, but scads of good CGI scripts exist online for the plucking. Most every Web server speaks some dialect of CGI, and almost all ISPs offer some level of CGI user support. So why should you even consider forsaking CGI for Java servlets?

The following chart compares and contrasts Java servlets with CGI scripts. Scores in each category range from very low to very high. Note that the picture this chart paints is, to some extent, subjective, since there are no hard-and-fast scoring rules in most categories. With that grain of salt:

  Java servlets CGI scripts
Ease of creation Medium Medium
Depending on their degree of functionality, Java servlets, like Java applets and applications, can be quite challenging to code. However, the basic model for servlets (as discussed on the next two pages) is a refreshingly simple and straightforward one. Tons of excellent Java IDEs exist, making your job as servlet creator potentially much easier. Again, bare-bones CGI scripts are really quite simple to code, but the difficulty increases exponentially with higher functionality. Since CGI is a specification (for data exchange) rather than a language, you can leverage your existing programming skills by coding CGI scripts in your language of choice: Perl, C/C++, Tcl, Visual Basic, and, yes, even Java.
Execution efficiency High Low
A servlet runs in a single process (within the server process). Thus, unlike CGI scripts, a new process does not have to be spawned for each new request for the servlet. This makes for low execution overhead and high efficiency. It also enables a servlet to handle multiple concurrent requests with ease and elegance. Moreover, since there is only a single instance of the servlet in existence (all concurrently running instances are actually threads of the main instance), server memory drain is minimized, and data persistence is enabled. A CGI script often spawns a new process every time it is invoked, thus causing a noticeable lag in transfer speed and potentially consuming great gobs of server processing time and resources. I say "often" because there are CGI technologies, such as FastCGI, that minimize script-invocation overhead. But even the best of these does not provide as effective of a solution as the one deployed by Java servlets.
Server support Medium High
Servlets can be supported on a large number of popular Web servers, but not all ISPs offer this support. CGI scripts can be, and are, supported by the great majority of existing Web servers. Harder to come by are ISPs that will allow users to upload their own custom CGI scripts (for obvious security reasons, since CGI scripts, like Java servlets, have the power to wreak system havoc).
Very high Medium
After all, we are talking about Java here: "Write once, run anywhere." Perl, when coded correctly, is almost completely platform independent. However, many other CGI-capable languages (such as Visual Basic) are not.
Availability Medium High
Online servlet repositories exist, but their number and breadth/depth of offerings do not rival those of CGI script repositories. Online CGI script repositories abound. From these, you can acquire custom CGI scripts to upload to your server. Or, if your server won't let you install custom scripts, you can take advantage of the many remotely hosted CGI scripts out there.
Security High Low
The strict Java servlet security model (Security Manager) and servlet sandbox safeguard your system from both intentional and accidental malicious servlet behavior. The mechanism of trusted (digitally signed) servlets allows for increased servlet capabilities. Since CGI scripts are not subject to the same degree/granularity of security sandboxing as Java servlets, they are significantly less secure.

Trouble deciding?
If you're comfortable programming in Java and your Web server supports servlets, go with servlets instead of CGI scripts, particularly if your application will be getting a large number of concurrent hits. Also, since servlets can avail themselves of all the standard Java API goodies, they are better than CGI scripts for large-scale server-side applications. And finally, if security is critical, stick with servlets.

If, on the other hand, Java is not your cup of tea, your Web server is servlet-shy, or you've found the perfect Perl script to tweak a bit and upload, go with the CGI.

There are of course other server-side technologies in use today: JSP (Java Server Pages, which extend servlets), ASP (Active Server Pages, on Microsoft servers), SSI (server-side includes), PHP, Python, server-side JavaScript, and more. JSP and ASP are competing behemoths and beyond the scope of this article, while the other languages are either significantly less powerful (SSI) or not as widely used (server-side JavaScript) as Java servlets and CGI scripts.



This information is from CNET. All rights acknowledged.

Rick Scott has been a programmer, tester, layout artist, instructor, multimedia developer, Web site designer, technical writer, and frequent contributor to ZDNet Developer. He thanks colleague and friend Jeff Sonstein for server-side Java expertise.

2020Media is a leading provider of small business Wi-Fi hotspots. If you want to offer free Wi-Fi to your customers or users, our service could be exactly what you are looking for. Our Hotspots are managed in the cloud, ultra-reliable and give you loads of information about behaviour patterns of your users. Read More   Read more...
2020Media introduces Moodle Hosting - UK servers and UK Moodle experts setup your site ready to use. Read more...
Special Offer for Longer Registration Periods Register or renew a .com, .net or .org domain name for 5 years we'll include 1 year free*. Read more...
2020Media is now offering zero-click installation of the popular TYPO3 content management system. Read more...

Our blog is regularly updated with news, tips and industry opinion.


  • AggieWestons
  • baker-and-mckenzie
  • arts-council-england
  • Big Finish
  • barratt-developments-logo
  • City-of-Lincoln-council-logo
  • brora-logo
  • DenisMacShane
  • Construction-Industry-Council
  • colchester