Servlet CGI (Common Gateway Interface) Servlets can link directly to the Web server. CGI cannot directly link to Web server. Servlets can share data among each other. CGI does not provide sharing property. Servlets can perform session tracking and caching of previous computations. CGI cannot perform session tracking and caching of previous computations. Servlets are portable. CGI is not portable. In Servlets, the Java Virtual Machine stays up, and each request is handled by a lightweight Java thread. In CGI, each request is handled by a heavyweight operating system process. Servlets automatically parse and decode the HTML form data. CGI cannot automatically parse and decode the HTML form data. Servlets can read and set HTTP headers, handle cookies, tracking sessions. CGI cannot read and set HTTP headers, handle cookies, tracking sessions. Servlets is inexpensive than CGI. CGI is more expensive than Servlets.
Difference between CGI and Servlet: CGI (Common gateway interface) Servlet 1. CGI is platform dependent Servlet is platform independent 2. CGI creates a new process for each request. Servlet creates a thread for each request and service the request in that thread. 3. CGI is not based on pooling (which hold no external resource but occupy memory i.e. not destroy after used) Its container managed pooling object. 4. CGI has no session tracking mechanism It supports session tracking mechanism 5. For each process created by CGI the process assign the separate address. so there is memory overload on the server For each thread created by Servlet no separate address space is created. All thread operates in the same parent address space (it is created when Servlet is load at first time). so there is no memory overload on the server 6. CGI Build with scripting languages (like C, C++, Perl etc) Servlet build with java
CGI:- 1.It is a ?process based? that is for every request a separate process will be created and that is responsible to generate the response.
2.Creation and destruction of new process for every request is costly. If the no. of requests increases, the performance of the system goes down. Hence CGI technology fails to deliver Scalable Applications.
3.Two processes never share the common address space. Hence concurrency never comes in CGI technology.
4.CGI programming can be written in multiple languages. Mostly common used languages PERL
5.Most of the CGI languages are not object oriented. Hence we may miss the benefit of the OOPS.
6.CGI technology is platform dependent.
Servlet:- 1.It is ?Thread based? that is for every client request a separate thread will be created and that is responsible for generation of dynamic response.
2.Creation and destruction of a new thread for every request is not Costly. Hence even though no. of requests increases there is no change in the response time and performance. Hence Servlet Technology succeeds to deliver Scalable Applications
3.All the Threads share the same common address space. Hence concurrency problems may rise.
4.Servlet should be written in Java only.
5.Java languages itself object oriented. Hence we can get all benefit of the OOPS.
In servlets, we are having a web page that is invoking servlets username and password. which is cheks in the database, Suppose the second page also. If we want to verify the same information whether it will connect to the database or it will be used previous information?