Difference between Java Servlet and CGI
A Servlet is a Java class that extends the capabilities of servers hosting applications accessed through a request-response model. Web servers primarily utilize servlets to extend hosted applications.
The Common Gateway Interface (CGI) serves as middleware between WWW servers and external databases and information sources. The World Wide Web Consortium (W3C) defined the Common Gateway Interface (CGI) and also defined how a program interacts with an HTTP server.
Java Servlet
- Servlets are thread-based, meaning that for every new request, a new thread is created.
- Servlet codes are written in the Java programming language.
- Since servlets are written in Java, they are object-oriented, providing the benefits of object-oriented programming (OOPs).
- Servlets are portable, meaning they can be used on different systems or platforms.
- Servlets remain in memory until explicitly destroyed.
- Servlets can be used with any web server that supports them.
- Servlets allow data sharing between components.
- Servlets link directly to the server.
- Servlets can read and set HTTP servers.
- The construction and destruction of new threads in servlets are not costly.
- Servlets may have a slower speed compared to other approaches.
- Servlets can be platform-independent.
Common Gateway Interface (CGI)
- CGI is process-based, meaning that for every new request, a new process is created.
- CGI codes can be written in any programming language.
- Since CGI codes can be written in any language, not all languages are object-oriented like Java, so the user may not receive the benefits of object-oriented programming.
- CGI is not portable, meaning it may not work on different systems or platforms.
- CGI is removed from memory after the completion of the process-based request.
- CGI can only be used with web servers that support it.
- Data sharing is not possible with CGI.
- CGI does not link the web server directly to the server.
- CGI cannot read or set HTTP servers.
- The construction and destruction of new processes in CGI can be costly.
- CGI generally has a faster speed compared to other approaches.
- CGI can be platform-dependent