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 request to the servers and servers return a response to the client. Once the client receives the response, connection between client and server will get lost. So even if the same client makes another request, server receives it as a new request from a new user, in other words server does not remember the client or the history of transactions with that client. If the request is for a static content like an HTML file or media file, 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 hard coded 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: www.oreilly.com

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 a 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 requests are 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 will create a request and a response objects. Then web container selects any thread from the thread pool and execute 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 destroy the request and response objects. 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 Servelt object for each and every requests.

To get a clear idea about dynamic contents 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 hard code 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.

Performance
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.

Security
Servlets are running inside the sand box 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 provides easiest error handling mechanism jointly with web container. CGIs do not have such a powerful error handling support.

Extensibility
Servlets are just Java classes so it is easy 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.
Previous
Next Post »

Contact Form

Name

Email *

Message *