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.
- When we need high performance and scalability in web applications
- When we need simple, language-independent server-side scripting
Servlet
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.
- Thread-based approach (one thread per request)
- Written in Java and runs on JVM
- Supports data sharing and persistence
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:

- A web server receives multiple client requests
- It creates a separate thread for each request (not a new process)
- Threads are lightweight and run concurrently
- All threads are handled by a single servlet instance
- Each request is processed independently by different threads
- One servlet can serve multiple users simultaneously
- Improves performance, speed, and scalability
CGI (Common Gateway Interface)
CGI is a standard protocol that allows web servers to interact with external programs to process user requests and generate responses.
- Process-based approach (new process per request)
- Can be written in multiple programming languages
- Less efficient due to process creation overhead
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:

- Each request creates a new process (CGI shell)
- Processes run independently
- Executes a CGI program for each request
- Process ends after execution
- Uses more memory and time
- Slower and less efficient
Servlet Vs CGI
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. |