Serving Dynamic Content - CGI vs Servlets

Note: This article focuses OCEWCD students, and the term server refers the combination of web server and Servlet container like Apache Tomcat.
In a web application, clients send a request to the servers and servers return a response to the client. Once the client receives the response, the connection between client and server will get lost. So even if the same client makes another request, the server receives it as a new request from a new user, in other words, the server does not remember the client or the history of transactions with that client. If the request is for static content like an HTML file or media file, the server is much happy with that request and return the file as the response. If the request is for a dynamic content like a web page which contains weather details on that day, or current time (Notice that these details cannot be hardcoded in a static HTML file. They have to be generated on runtime) a web server cannot handle that request; it has to be handled by any other helper applications. The helper application can be either a Common Gateway Interface (CGI) or Servlet.
Credits to:

Common Gateway Interface (CGI) is a standard method used to generate dynamic content on Web applications. CGI, when implemented on a Web server, provides an interface between the Web server and programs that generate the Web content. These programs are known as CGI scripts or simply CGIs; they are usually written in scripting languages but can be written in any programming language.

A Java servlet is a Java program that extends the capabilities of a server. Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET. Compared to CGIs, Servlets provide high performance, easy exception management, and portability. According to OCEWCD syllabus, the basic knowledge on CGI is more than enough and we will focus on Servlets.

In CGI every request is handled by separate processes, but for Servlets web container maintains a thread pool where a number of threads are waiting for new requests. Once the server receives a request, web container creates a response object. Then web container selects any thread from the thread pool and executes the Servlet’s service() method using that thread. The created request and response objects will be passed to the service method as parameters. Here the request object is used to give some input to the servlet and the response is used to receive outputs from the servlet. Then the container sends back the response to the browser and destroys the request and response objects. The container can decide to either destroy the allocated thread or to return it back to the thread pool. However, once a Servlet object is initialized it will be there in the RAM until the application is terminated. It reduces the time required to instantiate a new Servlet object for each and every requests.

To get a clear idea about dynamic contents to click on this link, enter your name on the available text box and click on the submit button.

Once you click the button it should display your name and location. Your location is identified using your IP address. To identify the location we need the users' IP address and it is available only at the runtime. It clearly shows that we cannot hardcode the location of users in an HTML file. This dynamic output is generated by a Servlet in this application.

Advantages of Servlets over CGI:
Platform independence
Servlets run on top of Java Virtual Machine (JVM) so they are platform-independent components. On the other hand, CGIs run on the operating system itself so they are platform dependent.

CGIs run on separate processes, so it takes more time to start a new process for each and every requests. Servlets are accessed through threads and those threads also being kept in a thread pool. It increases the performance.

Servlets are running inside the sandbox of JVM, so it is hard to damage the server side modules by malfunctioning the Servlets. Since CGIs are native applications, using CGIs a hacker can damage the server side components easily compared to Servlets.

Error Handling
Servlets provide easiest error handling mechanism jointly with web container. CGIs do not have such a powerful error handling support.

Servlets are just Java classes so it is easy to maintain and extend their functionality. All the object-oriented concepts can be directly applied to Servlets as well. CGIs are mostly written in scripting languages like Perl so the extensibility is very poor in case of CGIs.
Next Post »

Contact Form


Email *

Message *