Java Servlets, an introduction

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.

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 –

  • Whenever an end-user enters a web address into the web browser, the web browser generates an HTTP request for the same. This request is sent to a server which maps it to a particular Servlet. The appropriate Servlet is then dynamically loaded into the address space of the web server.
  • The init() method is invoked by the server and the method can be used to pass on the initialization parameters to the Servlet. The init() method is called only once in the entire life of the Servlet and it must be called before the Servlet can be used to service any request.
  • The service() method is then invoked so as to process the HTTP request and each request is to be serviced in its own separate thread. It can also be used to generate an HTTP response after reading and processing the HTTP request. Once a request has been serviced, the Servlet remains in the address space of the server and it is available to process other requests received from the clients.
  • The destroy() method is called to unload the Servlet from server’s address space. It relinquishes the resources allotted to the Servlet before taking it out of the service. The destroy() method is also called only once in the entire life of the Servlet.

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 –

  • A Servlet executes within the address space of the server and so, a separate process is not required to handle each request as there are N threads available for a single copy of the Servlet class. This results in better performance.
  • A Servlet can easily communicate with databases, applets or with other software through sockets as the full functionality of the Java class library is available to it. This makes Servlets very powerful when compared to the CGI programs. This also results in better performance.
  • Servlets inherit the safety features provided by the Java language. Java’s exception handling mechanism allows a Servlet to easily handle the errors. They’re also safe from the memory management problems because of Java’s automatic garbage collection feature.
  • Servlets are WORA programs i.e. write once, run anywhere programs as they’re written in Java. The platform-independent feature of Servlets is a great advantage as it allows us to create Servlets that can be used on the servers with different operating systems and different implementations.
  • A Servlet is also benefited with the Java Security Manager which enforces a set of restrictions so as to protect the resources of the server. This makes Servlets more secure when compared to the 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.


Don't like it? There are lots of published articles, pick a random one.

oCricket

Brajeshwar posted this article on Tue, Jun 24th, 2008 at 7:06 pm
Categorized under Java and has the following tags

Prev Article: Open Source RIA books site - toString

Next Article: The ‘end task’ procedure for Linux

Archives: Visit the Archives for more articles.

Comments Post Yours

There are one response so far. You can follow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site.

  1. Pings & Trackbacks Sites, articles & blog posts linking back to this article.

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

Great Indian Developer Summit 2009

I got a Press Release of the upcoming GIDS '09 and here is an excerpt. The summit's program covers Java, REST, Unit testing, Groovy, Spring, Struts 2.0, SOA, Cloud Computing, Web Services, JRuby, RoR, Ruby, JVM, ...21st Jan, 2009

The flourishing gun market in Pakistan

VICE Travel: Darra, Pakistanby Top-Notch112 (Via: Deep Green Crystals) 20th Jan, 2009

Angry Ringtone for iPhone and others

[audio:http://audio.brajeshwar.com/angry-ring-ring.mp3] The ANGRY RINGTONE for iPhone. (Click the PLAY button above!) Download * iPhone Ringtone (.m4r) * MP3 Ringtone (.mp3) * Zipped (both .m4r and .mp3) To use it as an iPhone Ringtone; just double click the file "angry-ring-ring.m4r" and it ...15th Jan, 2009

IIM Ahmedabad's Leverage 2009

Leverage, the Venture Capital and Private Equity Club of IIM Ahmedabad and the Centre for Innovation Incubation and Entrepreneurship bring to you the 1st edition of the Venture Capital and Private Equity Conference on the ...12th Jan, 2009

Fedora 10 Released

Let's talk history! Fedora was created in late 2003 as a Linux based Operating System which gives the users an admittance from anywhere in the world to the most up-to-date free and open source software ...28th Nov, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • Not Safe for Work Ever clicked a link and felt embarrassed with the content in front of your co-workers? Ever caught unaware because the funny link your friend sent was a little beyond funny? Let’s minimize that with NSWF.
  • My Special Job My Special Job is a place where you can look for your weird necessities, strangely superb employees, when your need are more of those hackers, geeks, and ultimate rockstars in the Internet Technology.
  • Downloads All downloads, Free and Open Source.
  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications. More about me.

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 949 Articles and 6,472 comments, contained within 22 categories and 1,497 tags.