![]() |
VOOZH | about |
Servlet and CGI are server-side technologies used to handle client requests and generate dynamic web content. While both follow the request-response model, they differ in performance, architecture, and execution approach.
A Servlet is a Java class used to handle client requests and generate dynamic responses on the server. It runs inside a web server using threads, making it efficient and scalable.
Real-world Example: When a user submits a login form, the servlet processes the request, validates data, and sends a response.
The diagram shows how a Servlet handles multiple requests using threads:
CGI is a standard protocol that allows web servers to interact with external programs to process user requests and generate responses.
Real-world Example: A web form submits data to a CGI script, which processes it and returns a result page.
Below diagram shows how CGI (Common Gateway Interface) handles multiple client requests:
The following table explains the difference between the servlet and CGI:
| Basis | Servlet | CGI |
|---|---|---|
| Approach | It is thread based i.e. for every new request new thread is created. | It is process-based i.e. for every new request new process is created. |
| Language Used | The codes are written in JAVA programming language. | The codes are written any programming language. |
| Object-Oriented | Since codes are written in Java, it is object oriented and the user will get the benefits of OOPs | Since codes are written in any language, all the languages are not object-oriented thread-based. So, the user will not get the benefits of OOPs |
| Portability | It is portable. | It is not portable. |
| Persistence | It remains in the memory until it is not explicitly destroyed. | It is removed from the memory after the completion of the process-basedrequest. |
| Server Independent | It can use any of the web-server. | It can use the web-server that supports it. |
| Data Sharing | Data sharing is possible. | Data sharing is not possible. |
| Link | It links directly to the server. | It does not link the web server directly to the server. |
| HTTP server | It can read and set HTTP servers. | It can neither read nor set HTTP servers. |
| Cost | Construction and destruction of new threads are not costly. | Construction and destruction of the new processes are costly. |
| Speed | Its can speed is slower. | It can speed is faster. |
| Platform dependency | It can be Platform Independent | It can be Platform dependent. |