Servlet Tutorial

This section describes you what is Servlet, use of Servlet, Servlet container, Servlet API, Servlet life cycle, versions of Servlet, Servlet Container Servers

Servlet Tutorial

Servlet Tutorial

In this section we will discuss about Java Servlet.

This section describes you What is Servlet, Use of Servlet, Servlet container, Servlet API, Servlet life cycle, versions of Servlet, Servlet Container Servers

What is Servlet ?

Servlet is a part of J2EE. As we know, in Java, J2EE is used to develop web based applications, web services, enterprise software etc. Servlet is a Java based API which runs on Server machine. A Servlet is intercepted on a request it receives, and generates a response according to the corresponding request. Servlet is a Java class that extends the capabilities of a Server. Servlet can be thought of as an Java Applet but, the difference between Servlet and Applet that the Servlet runs on Web Server whereas Applet runs on Web Browser.

Use of Servlet

  • Servlet is used to store and process the HTML form data.
  • Servlet is used to provide dynamic content.
  • Servlet is used to manage state information.

Servlet container

Servlet container is also called a web container, is used to deploy and run Servlet. The Servlet container is a component of Web Server which interacts with the Servlet . The Servlet containermanages the Servlet's life cycle, maps the URL etc.

Servlet APIs

To use Servlet for developing the Java based web applications, web services, etc. following package is given :

  • javax.servlet : This package specifies the required interaction of web container and a Servlet.
  • javax.servlet.http : This package specifies the HTTP specific interaction as well as session management.

Servlet life cycle

Life cycle of Servlet is managed by the three methods named init(), service(), and destroy(). These methods are implemented by every Servlet and they are called at specific time by the server.

  • init() : When a Servlet is called this method is invoked first by the web container. A web container invoked this method to initialize the Servlet instance. This method is called only once in the life cycle of Servlet.
  • service() : Once a Servlet is initialized it can service the client request. A web container calls the Servlet's service() method for every request. To provide the service of each request a separate thread is created.
  • destroy() : When a Servlet is required to stop the service a web container calls this method and the Servlet becomes out of service. This method is called only once in the Life cycle of Servlet.

Versions Of Servlet

Servlet has been released with various versions. Current version of Servlet (at the time of writing this section) is Servlet 3.0. Prior versions of Servlets are listed below :

Servlet Container Servers

Now a days there are various Servlet container servers. Some of the famous Servlet container servers are as follows :

  • Glass Fish
  • IBM WebSphere Application Server
  • Jetty (Web Server)
  • Apache Tomcat