Turn Desktop View Off
You are here:   HomeJava Servletintroduction to ServletJsp To Servlet Communication
by Dinesh Thakur Category: introduction to Servlet

The Common Gateway Interface (CGI) is the first technology used to generate dynamic contents. It allows a web client to pass data to the application running on the web server so that a dynamic web page can be returned to the client according to the input data. For example, when you use a search engine, buy a book at an online store; get a stock quote etc., your browser uses CGI to communicate with a server side application.

CGI is not a programming language, rather it is an interface (or a set of rules) that allows an input from a web browser and produce an output in the form of HTML page. A CGI script can be written in a verity of languages such as C, C++, Visual Basic, Perl, FORTRAN and even Java. Out of these, Perl is most commonly used.

When a web server receives a request for a CGI script, the web server passes some parameters to this script and executes it. The script runs and generates some output which is then collected by the web server and returned to the client (browser).

CGI request response cycle

Advantages of CGI

1. CGI is a true cross-platform technology. This means that CGI scripts work with any web browser as well as with most web servers running on Windows and Unix.

2. CGI is language independent. It can be written in a variety of languages so developers do not have to learn a new language.

3. CGI is very simple interface. It is not necessary to have any special libraries to create a CGI program or write programs using a particular API.

Disadvantages of CGI

1. A new process is started for each client request. The creation of the process for every such request requires time and significant server resources which limits the number of requests a server can handle.

2. CGI program cannot interact with the web server or take advantage of the server's abilities once it begins execution. This is because it is running in a separate process. For example, a CGI script cannot write to the server's log file.

Difference between SERVLET and CGI



Both Java servlets and CGI are used for creating dynamic web applications that accept a user request, process it on the server side and return responses to the user. However, Java servlets provide a number of advantages over traditional CGI which are as follows,

• Efficient: Unlike traditional CGI where a new process is started for each client request, a servlet processes each request as a thread inside of a process. Thus servlets improve the performance as it removes the overhead of creating a new process for request every time.

Also, unlike CGI program which terminates after handling a request, the servlets remains in memory even after they complete a response and destroyed only when the servlet container is shutdown. Thus servlets make it easier to cache computations, keep database connections open and perform other optimizations that rely on persistent data.

Powerful: Servlets support several capabilities that are difficult or impossible to accomplish with traditional CGI. These capabilities include talking directly to the web server, sharing data between multiple servlets, session tracking and caching of previous computations.

Portable: Since servlets are written in the Java programming language and follow a standard API, so a servlet can be moved from one servlet compatible web server to another very easily. For example, servlets written for Apache web server can run unchanged on other severs such as Microsoft Internet Information (IIS) Server, IBM Web Sphere etc. On the other hand, CGI programs may be platform dependent, need to be recompiled or web server dependent.

Inexpensive: A number of free or very inexpensive web servers are available these days. Once you have a web server, adding servlet support to it costs very little. On the other hand, CGI alternatives require a significant initial investment to purchase a proprietary package.

Language dependency: Since servlets can be written only in Java so they are language dependent. On the other hand, CGI programs can be written in any programming language like C, C++, Perl, Visual Basic or even Java so they are language independent.

Secure: As Java was designed from the ground up as a secure language, so a servlet can be run by a servlet engine or servlet container in restrictive sandbox just like an applet runs in a web browser's JVM, which increases the server security. On the other hand, CGI scripts are not subject to same degree of security sandboxing as Java servlets, they are significantly less secure.

Convenient: Servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, session handling etc. On the other hand, CGI does not support such infrastructure.



What's New and Popular





Search Content







Advance Courses



Basic Courses



Advertise with Us