In principle Servlets are always better than CGIs. But since CGIs have been around for such a long time, there's more of them in the public domain. And any Web Application that has many Servlets with a lot of HTML in println statements may need to be redesigned. Use JSPs for presentation (View) instead, and Servlets for the Controller (MVC pattern). My $0.02
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
The need to generate HTML code (in println or otherwise) is also present in CGI. From that point of view they are the same. The drawbacks of servlets are few, but they include: 1. You need a special "servlet container" to run servlets, and most web servers (and thus most public hosting companies) don't include this. 2. You need a Java Runtime Environment on the server to run servlets. CGI is a completely language independent protocol, so you can write CGIs in whatever languages you have available (including Java if you want to). 3. Sometimes there are no Java interfaces to particular resources or devices. In this case writing a CGI in a language which does have native support for access to these resources or devices can be a lot simpler than setting up JNI to call through another language. And it might not even be possible through JNI. 4. Although each CGI requst usually requires a process start (which can make them a lot slower than servlets for short tasks) a CGI can be written in a fully compiled language and use fully compiled and optimized low-level system calls, so complex, long, or machine-dependent processing can be faster and simpler using CGIs 5. You can easily test a CGI on the command-line of the server: just set a few environment variables and run it to see the output. This is not easy with servlets.
Don't be discouraged, though. The list of advantages for servlets is much longer ...