Introduction to Servlet and Advantages of Servlet over Common Gateway Interface (CGI) - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Introduction to Servlet and Advantages of Servlet over Common Gateway Interface (CGI)

Share This

The Servlet and JSP environment extends the need for basic Java support. Any computer running JSP or Servlets needs to also have a container. 


A container is a piece of software responsible for loading, executing, and unloading the Servlets and JSP. 


The reasons for this are largely related to the history of server-side Web development. A quick overview of one of the earliest and most prominent server-side dynamic content solutions, CGI, and the differences between it and Servlets are very helpful in understanding why a JSP/Servlet container is required.



Common Gateway Interface (CGI)


The Common Gateway Interface, or CGI, is commonly referred to as one of the first practical technologies for creating dynamic server-side content. With CGI a server passes a client's request to an external program. This program executes, creates some content, and sends a response to the client. 


When first developed, this functionality was a vast improvement over static content and greatly expanded the functionality available to a Web developer. Needless to say, CGI quickly grew in popularity and became a standard method for creating dynamic Web pages. However, CGI is not perfect.


CGI was originally designed to be a standard method for a Web server to communicate with external applications. As designed, each request to a CGI resource creates a new process on the server and passes information to the process via standard input and environment variables.

While it does work, the CGI life cycle is very taxing on server resources and greatly limits the number of concurrent CGI users a server can support. 


It takes a noticeable amount of time to start and stop the entire process. A better solution is to start the server process once, handle all requests, and then shut it down when there is no longer a need for a Web server. 


Starting and stopping a Web server is like the single-phase life cycle of CGI, and it was a very noticeable problem. CGI did not scale well and would often bring a Web server to a halt.


The Second-generation CGI implementation attempted to counter the performance problems of the original CGI, namely FastCGI. While the single-phase life cycle still existed, CGI implementations improved efficiency by pooling resources for requests. This eliminated the need to create and destroy processes for every request and made CGI a much more practical solution. The following figure shows the improved implementation of CGI. 

But still, it was not without problems. The most notable problem is the difficulty in sharing resources such as a common logging utility or server-side object between different requests. 



Servlet


In the Java world, Servlets were designed to solve the problems of CGI and create robust server-side environments for Web developers. Similar to CGI, Servlets allow a request to be processed by a program and let the same program produce a dynamic response. 


Servlets additionally defined an efficient life cycle that included the possibility of using a single process for managing all requests. This eliminated the multi-process overhead of CGI and allowed for the main process to share resources between multiple Servlets and multiple requests

The Servlet diagram is similar to that of second-generation CGI, but notice all the Servlets run from one main process, or a parent program. This is one of the keys to Servlet efficiency, and, as we will see later, the same efficiency is found with JSP. 


With an efficient design and Java's cross-platform support, Servlets solved the common complaints of CGI and quickly became a popular solution for dynamic server-side functionality. 


Servlets are still popular and are now also used as the foundation for other technologies such as JSP. Currently, Servlets and JSP combined make up the official "Web Tier" for the Java 2 Enterprise Edition, J2EE.



CGI vs. Servlet

Before we dive deeper into the servlets, let us know that before servlet technology was there in the market, there was another technology called CGI (Common Gateway Interface) which could run programs in the server environment. Servlet technology, very rapidly, intruded into the market of CGI, and nowadays, it has completely won over CGI technology. Our idea of Servlet will get much clear if we get to know a little history of why and how Servlet Technology got just better than CGI. This has been explained next.

CGI is a Process-Based Technology. That means whenever a client makes a new request for the CGI server, the server spawns a whole new process to serve the request. Thus with the increasing number of client requests, the number of processes inside the CGI servers increases which adversely affects the performance of the server.

But as Servlet is a Thread Based Technology, with an increase in the number of client requests, the server does not spawn new processes, rather it creates new threads to serve those requests with the servlets. Hence it does not let the performance degrade as always threads can share the common computing resources.

The CGI was built on top of some platform-dependent languages like C, C++, and others, whereas Servlet Technology is built on top of Java, a platform-neutral language. The other aspects like securities, portability, and robustness are all more enhanced and advantageous in the case of Servlet than CGI as the former is backed by Java.

Happy Exploring!

No comments:

Post a Comment