First let us know the earlier Web technologies, their drawbacks and how they are overcome in Servlets.
Earlier to Servlets, the other Web technologies existed were CGI/PERL and ASP.
Servlets vs CGI vs ASP
I. CGI/PERL (Common Gateway Interface/Practical Extraction and Report Language)
CGI/Perl is process based. For every request a separate process is started. Every process requires a separate PERL software loaded. For example, 100 requests arrive for different CGI programs, 100 PERL interpreters are loaded and 100 different processes are started. This will consume more of RAM space and load on the processor increases if more and more requests starts coming. For this reason, the Web master keeps a limit over the usage of RAM for CGI requests, say 1000. If more than 1000 comes, they must wait in a queue until a few were honoured. The net result is late response to client.
Drawbacks of CGI/PERL tabulated
- RAM space is wasted.
- Overhead on the processor increases
- Late response to client
There was no other way but for CGI in those days.
Servlets vs CGI vs ASP
II. ASP (Active Server Pages)
The problems with CGI/PERL were studied by Microsoft and released ASP as a replacement to CGI. The advantage of ASP is it is thread based (remember, CGI is process based). One IIS (Internet Information Server) server software is enough to honour any number of client requests. It was most wanted in those days. But it comes with many drawbacks (the drawbacks of almost every earlier Microsoft product; one product is designed to be dependable on other Microsoft product).
Drawbacks of ASP
- ASP works on Windows OS only. But maximum servers on Web work, even as-on-today also, on LINUX. They placed a separate system for Windows and loaded IIS. Now the request comes for a LINUX server, the request is routed to Windows server. The response is routed back to client through Windows and LINUX. Routing is extra process overhead.
- ASP works on IIS server only, developed by Microsoft only.
- Written in VBScript, another Microsoft product.
These drawbacks were fully overcome in Servlets. Servlets come with lot of advantages and most demanded in the market as-on-today.
Servlets vs CGI vs ASP
III. Advantages of Servlets
All the drawbacks of earlier technologies CGI/PRRL and ASP are overcome in Servlets.
- It is potable. It is written in Java and Java is platform-independent, Servlets are also platform-independent,
- Works on any OS (ASP is Windows dependent).
- Multithreaded (ASP also supports threads). Threads are lightweight as all threads are spawned from the same process.
- Servlets are efficient. Java is object-oriented whereas PERL and VBScript are object-based languages. All the drawbacks of object-based languages, like array and string bounds checking, unassigned variables filled with garbage values, are overcome in Servlets. Another Java feature, Generics is a big boon to Servlets.
- Abundant Web servers support. Sun Microsystems supplied the Servlet API to all the vendors to develop Web servers that execute Servlets. Many came in the market with their own implementations like Tomcat, Weblogic, WebSphere, Jetty etc. The software companies has a choice to prefer their own fancy servers of less cost. Infact, Tomcat is a free server and most demanded (ASP works only on IIS).
- Inter Servlet communication. One thread data can be shared by other thread. Even if one thread is killed, still the process will be working with other threads. Here, a thread represents a request. With CGI, one process cannot make use of other process data as when response is delivered the process stops.
- Servlets are convenient with high-end utilities like HTTP headers, Cookie support and session management etc.
- Servlets are powerful. Some capabilities exist with Servlets which are difficult in CGI to do. For example, communicating with the Web server. Servlets can communicate directly with the container where as in CGI difficult and requires extra API (which again server specific).
- Servlets are secure as they are executed by JVM and Web container with sandbox security. CGI programs are not much secured as they are executed by OS shells.
- Servlets are inexpensive for the reason the Web servers that execute Servlets are available free-of-cost like Tomcat of Apache with commercial quality.
- Servlets come with special features (Java built-in) like exception handling and garbage collection.
- Writing dynamic web pages is easier to Programmer and also faster to run by the container.
- Support for communication. Container communication and Inter servlet communication are easier with built-in APIs.
- Support for Sessions. Due to stateless and connectionless HTTP protocol, the Servlets come with its own API like HttpSession interface to support session tracking.
- Support for new frameworks like MVC Struts and Spring. Lot of frameworks are existing in now-a-days market that makes Servlets simple to practice.
Disadvantages of Servlets
Disadvantages are very trivial to mention.
- You are required to know an OOPs language Java indepth. Learning Java being object-oriented, will be cumbersome compared to object-based PERL and VBScript.
- With complex business logic, sometimes Servlets become difficult to read and understand for others.
- Servlets are language dependent and should be written in Java only whereas CGI is language independent; ofcourse, PERL is preferred mostly due to text manipulation regular expressions.
Later JSP was released by Sun Microsystems. JSP is still easier than Servlets. Infact, JSP is internally converted to a Servlet and executed.