Introduction To Java Servlet Technology

In this tutorial you will learn about the Java servlet technology and why it came into existence, what are its features etc.

Introduction To Java Servlet Technology

In this tutorial you will learn about the Java servlet technology and why it came into existence, what are its features etc.

Introduction To Java Servlet Technology

Introduction To Java Servlet Technology

In this tutorial you will learn about the Java servlet technology and why it came into existence, what are its features etc.

Java Servlet is a technology to generate the dynamic content on web for delivering services. However before the Servlet, CGI server-side scripts were used as the main technology to generate the dynamic content for delivering services, but it had many drawbacks like platform dependencies, lack of scalability, more execution time, etc. Servlet has overcome these drawbacks and came into existence. Servlet has become so popular because it is specialized in browsing the web for data mining, information extraction, meaningful presentation inside the web browser.

Servlet : Servlet is a just a class written in Java Programming Language to use the server potentialities that hosted applications which are accessed on the basis of request and response model. Servlets are highly modular and can do several different operations due to its object-oriented feature.

There are two packages javax.servlet and javax.servlet.http that provides the interfaces and classes using which servlet class can be written. Servlet interface is implemented by the all the servlets. This servlet defines the method of servlet life cycle(discussed later). When you will create your own servlet and also implementing the generic services then the inheritance hierarchy will be as follows :

Purpose of extending the HttpServlet class is to provide the HTTP specific services to your servlet. This is done by the methods doGet(), and doPost() of HttpServlet class.

Servlet Life Cycle

The servlet container into which the servlet has been deployed, manages the servlet life-cycle. The servlet container does the following steps when a servlet encountered a request :

  1. In the first case if the servlet instance is not existed then web container do the following tasks :
    • Loads the Servlet class.
    • creates instance of the servlet class.
    • calls the init() method for initializing the servlet class instance.
  2. Passes the request and response object by invoking the service method i.e. doXXXX() (doGet(), doPost(), doPut() etc) methods.
  3. Destroys the servlet i.e. call the destroy() method.

In summary life cycle of servlet is responsible for its loading, instantiating & initializing, controlling request & response and end of service. Following fig. shows the life-cycle of servlet :

It should be remembered that init() & destroy() method is called only once in the whole life cycle of a servlet.

Servlet life-cycle events can be controlled by defining listener object's, for this listener class must be defined and specified.