Java Servlets, an introduction

We’ve been talking a lot about Linux lately. Let’s break out a bit from that and talk about another topic – the Java Servlet Technology. We’ll try to cover some basic aspects of Java Servlet Technology and may be a little more.

To give you a basic idea, Servlets are the server side components that are the Java counterpart to dynamic web content technologies like CGI, PHP and ASP.NET.

A lot of techniques have been developed to overcome the problems associated with Common Gateway Interface (CGI) and Servlets lie among them. Now, if you’re unsure of the problems associated with CGI then you need to move back to the early days of the Web. In the early days of Web, a server could construct a page dynamically but a separate process was required to handle each request, separate processes were required to read the data from the HTTP request and to write the data to the HTTP response, separate processes were required to communicate with the database. All these activities required a lot of server resources and hence, resulted in the development of programs that were not able to deliver the expected performance.

Servlets were introduced to overcome these problems and now, Servlets have gained a lot of popularity among developers for creating a number of server applications. So, what’re Servlets? Servlets are the small programs that are run on the server side of the connection and they’re used to dynamically extend the functionality of a web server. They follow the standard framework, use portable Java language and so, developers can use Servlets for creating efficient server side applications that can run on any Servlet enabled web server.

As such, Servlets are not designed for any specific protocol but they’re mainly used for processing HTTP requests. It uses the classes available in the Java packages – javax.servlet.http and javax.servlet. In addition to this, Servlets run inside the Java Virtual Machine and they enjoy the access to the entire family of Java APIs.

A Servlet is basically a Servlet class that implements the Servlet interface. Specific methods are to be implemented in order to create a Servlet meant for processing client requests and it is the web developer’s duty to carefully implement these methods. If the methods are not implemented to process a certain request then an error is generated when that particular request is encountered.

Life Cycle of a Servlet

init(), service() and destroy() methods are central to the life cycle of the Servlet as they’re implemented by every Servlet and these methods are called at specific times during the life cycle of the Servlet.

Here’s a typical scenario so as to understand when these methods are called –

As Servlets are written in Java, they offer a number of advantages over CGI programs which suffer from serious performance problems. Some of the advantages of using Servlets over CGI Programs –

To conclude, Servlets offer a number of benefits and enjoy the access to the entire family of Java APIs which allow web developers to use Servlets for creating a number of server side applications meant for serving different purposes. These days, Servlets are a popular choice among developers for creating portable, secure, robust, interactive applications.

Well, there’s a lot to be covered for Java Servlets but for now, I believe you must have gained some insight into what’re Servlets and what’re the benefits of using Servlets. I’ll be writing more articles discussing the various interesting aspects of Java Servlets so keep reading this site.