Servlet is a small Java program that runs within a Web server.Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol.
Common Gateway Interface (CGI) is a standard environment for web servers to interface with executable programs installed on a server that generate web pages dynamically.
||CGI cannot directly link to Web server.|
||CGI does not provide sharing property.|
||CGI cannot perform session tracking and caching of previous computations.|
||CGI is not portable.|
||In CGI, each request is handled by a heavyweight operating system process.|
||CGI cannot automatically parse and decode the HTML form data.|
||CGI cannot read and set HTTP headers, handle cookies, tracking sessions.|
||CGI is more expensive than Servlets|
Both are server-side technologies, but CGI programs execute in their own shell, at run-time they create there own shell, and it involves an heavy weight activity. but where as servlets can be requested using different threads.
Advantages of CGI:
Can be written in variety of languages ,primarily written in perl
Bugs does not crash the web server
Easy for reference
Execute in their own OS Shell, doesn’t have concurrency conflicts
All service providers support CGI Programs
Response time is high, the creation of an OS Shell is an heavy weight activity
CGI is not scalable
Not always secure or object-oriented
No separation of presentation and business logic
Scripting languages are often platform-dependent
Advantages of servlets over CGI
Request is run in a separate thread,so faster than CGIs
Scalable,can serve many more requests,
Robust and Object Oriented.
Can be written in Java Programming language.
Access to Logging Capabilities.
Error handling and Security.
1.CGI creates a new process for each request Whereas Servlet creates a thread for each request and services the request in that thread.
2.For each process created by CGI the process is assinged seperate address space.SO there is memory overload on the server.Whereas for every thread created by the servlet no seperate address space is created all threads operate in the same parent process address space.so there is no memory overlaod.
3.CGI is not based on pooling Whereas servlet are container managed pooled objects.
cgi and servlets are used to develop webapplication only.cgi is a oldest technique .the main drawback of cgi is
- starting process is very expensive
it performs very poor.
3) error handling,
4) session management, and
5) access to and ability to extend the server itself.
Servlets are written entirely in java so these are platform independent. Servlets can run on any Servlet enabled web server. For example if you develop an web application in windows machine running Java web server, you can easily run the same on apache web server (if Apache Serve is installed) without modification or compilation of code. Platform independency of servlets provide a great advantages over alternatives of servlets.
Due to interpreted nature of java, programs written in java are slow. But the java servlets runs very fast. These are due to the way servlets run on web server. For any program initialization takes significant amount of time. But in case of servlets initialization takes place first time it receives a request and remains in memory till times out or server shut downs. After servlet is loaded, to handle a new request it simply creates a new thread and runs service method of servlet. In comparison to traditional CGI scripts which creates a new process to serve the request.
Java Servlets are developed in java which is robust, well-designed and object oriented language which can be extended or polymorphed into new objects. So the java servlets take all these advantages and can be extended from existing class to provide the ideal solutions.
Java provides very good safety features like memory management, exception handling etc. Servlets inherits all these features and emerged as a very powerful web server extension.
Servlets are server side components, so it inherits the security provided by the web server. Servlets are also benefited with Java Security Manager .