Java Servlets

Java Servlets provide a dynamic, component-based, platform-independent technique for developing web-based applications, without the performance limitations of CGI (e.g. Perl, C++ etc.) programs. Servlets are the foundations of web application development using Java and they provide web developers with a simple, consistent mechanism for extending the functionality of a web server and for accessing existing business systems. They have access to the entire family of Java APIs, including the JDBC API to access enterprise RDBMSs (e.g. Oracle, IBM DB2, MS SQL Server, MySQL, MS Access, SAP Adaptive Server Enterprise etc.).

 

Advantages of Servlets

The traditional way to add functionality to a web server is the Common Gateway Interface (CGI), a language-independent interface that allows a server to start an external process which acquires information about a request through environment variables, the command line and its standard input stream and writes response data to its standard output stream. Each request is answered in a separate process by a separate instance of the CGI program, or CGI script (written in Perl or C++).

Java Servlets have several advantages over CGI programs:

  • A Servlet does not run in a separate process (instead it creates thread). This eliminates the overhead of creating a new process for each HTTP request.
  • A Servlet stays in memory between client requests. A CGI program should be loaded and started for each CGI request.
  • There is only a single instance which responds to all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
  • A Servlet can be run by a Servlet engine (i.e. Servlet container) in a restrictive sandbox (similar to an Applet that runs in a web browser’s sandbox) which allows secure use of untrusted and potentially harmful Servlets.
 
Servlet (and JSP also) have the following advantages over other technologies :–
  • Performance. The performance of Servlets is superior to CGI because there is no process creation for each client request. Instead, each request is handled by the Servlet container process. After a Servlet is finished processing a request, it stays resident in memory, waiting for another request.
  • Portability. Similar to other Java technologies, Servlet applications are portable. We can move them to other operating systems without serious hassles.
  • Rapid development cycle. As a Java technology, Servlets have access to the rich Java library, which helps speed up the development process.
  • Robustness. Servlets are managed by the Java Virtual Machine. As such, we don’t need to worry about memory leak or garbage collection, which helps us write robust applications.
  • Widespread acceptance. Java is a widely accepted technology. This means that numerous vendors work on Java-based technologies. One of the advantages of this widespread acceptance is that we can easily find and purchase components that suit our needs, which saves precious development time.
  • Secure. Servlets are server side components, so it inherits the security provided by the web server. Servlets are also benefited with Java Security Manager.
  • Extensibility. The Servlet API is designed in such a way that it can be easily extensible. As it stands today, the Servlet API supports HTTP Servlets, but in later date it can be extended for another type of Servlets.