Java Servlet vs CGI

Dear all,

Can anyone tell me why Java servlet is better than CGI?
benson031397Asked:
Who is Participating?
 
masatoConnect With a Mentor Commented:
CGI is using platform-specific APIs, but Servlet is using Java. Which means "Write Once, Run Any Server"
Servlet can handle multiple requests, and can synchronize requests.
0
 
heyhey_Commented:
usually CGI starts another process.
servlet uses just another Thread.
0
 
AstromanCommented:
As stated, a new process is required for each CGI request. This means new stack frame, etc and therefore lots of resources. Servlets have only one copy running (usally. A servlet can be aliased to a diffenent name which will run independently), and all servlets share a single virtual machine.

CGI is usually written in interpreted languages(i.e. sh, or perl) which are slower than native apps. This can be avoided by writing in something like C, but then it is machine dependent. Servlets are machine independent and run nearly as fast as native code. Because the process is only started once the startup overhead can be disregarded. Not so with CGI.

CGI is also less secure than servlets. Servlets run in a secure sandbox, so shouldn't be able to do any damage outside of the domain of the servlets. The worse thing that can happen is that you can bring all the servlets down. With perl CGI (for example) it's possible to trick badly written scripts into executing arbitrary chunks of code. Very dodgy.

Are they good enough reasons to use servlets over CGI?

Leon
0
 
aziz061097Commented:
Another reason I can think of is database connection , a CGI program has to make a connection to the database for every request (if it requires database results) whereas a servlet can maintain the connection for various requests . Database connection time is a very costly (time consuming) operation .
0
 
AstromanCommented:
Come on benson, I think we've all given you some good answers, how about dishing out some points!
0
All Courses

From novice to tech pro — start learning today.