Servlets Tutorial

A part of JEE, Servlets is a web programming technology used by Java programmers.

Servlets Tutorial

Learn servlets with this most complete Servlets tutorial in an easy and fun way. Helping you learn all the basic and advance concepts of Servlets, this tutorial will help you develop your web based application quickly and effectively with practical examples and programs.

Who is this Servlets Tutorial designed for?

This tutorial is designed for Java Programming beginners and professionals who want to learn developing their web applications with Servlets. All freshers, BCA, BE, BTech, MCA and college students will find this tutorial very helpful in taking their skills many steps ahead. You can use also use this tutorial to develop your notes, exam preparation, lab exercises, assignments and viva questions. If you are a professional, feel free to skip to the advance chapters.

What do I need to know to begin with?

To learn Servlets effectively you should have your concepts of Java programming and web application development in place. Good knowledge of how Internet works will be an added advantage.

Servlets syllabus covered in this tutorial

This tutorial covers:

Servlets Life Cycle, Developing Servlet Application, Request & Response Interface, ServletConfig Interface, ServletContext Interface, Attribute, Servlets, Session Tracking, Cookies, Filter, Database Access, RequestDispatcher, File Uploading.

Excited to learn? Let's begin!


  • Java Servlets is web programming technology in Java.
  • A Servlet is used to enhance client-server programming model and develop web applications.
  • Servlet can respond to any type of request. Java Servlet technology defines HTTP specific Servlet classes.
  • Java Servlet is a part of Java Enterprise Edition (Java EE).
  • The javax.servlet and javax.servlet.http package provides the interfaces and classes for writing Servlet program.

Web Application

An application that is accessible from the web is known as web application. A web application is a client-server application, which runs on web browser. It can contain many web components like Servlet, JSP, and Filter etc.

Servlet API

The Servlet API contains two important packages that provide all important classes and interface.

These are as follows:
1. javax.servlet
2. javax.servlet.http

Important Classes and Interfaces of javax.servlet package:


Important Classes and Interfaces of javax.servlet.http package:


Servlet Interface

Servlet interface defines some methods that all the servlet classes must implement. This method provides the following five methods. Out of these five methods, three methods are Servlet life cycle methods.

Servlet Interface Methods
Following are the methods of Servlet Interface:

public void init(ServletConfig, config)It is one of the Servlet life cycle methods. It is invoked by Servlet container after being initialized by Servlet.
public void service (ServletRequest req, ServletResponse res)The service( ) method is called after successful completion of init( ). It is invoked by Servlet container to respond to the requests coming from the client.
public ServletConfig getServletConfig( )Returns a ServletConfig object, which contains initialization and startup parameters for this Servlet.
public String getServletInfo( )Returns the information about the Servlet, such as author, version, and copyright. This method returns a string value.
public void destroy( )Called by servlet container and it marks the end of the life cycle of a servlet. It indicates that servlet has been destroyed.

HttpServlet Class

The HttpServlet class extends the GenericServlet and implements Serializable interface. It is an abstract class. The HttpServlet class reads the HTTP request from http, get, post, put, delete etc. It calls one of the corresponding methods.

HttpServlet Class Methods
  • protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  • protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
  • protected void doHead(HttpServletRequest req, HttpServletResponse resp)
  • protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  • protected void doPut(HttpServletRequest req, HttpServletResponse resp)
  • protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
  • protected void service(HttpServletRequest req, HttpServletResponse resp)
  • public void (ServletRequest req, ServletResponse resp)

GenericServlet Class

It is an abstract class that implements Servlet, ServletConfig and serializable interface. It provides the implementation of all methods of these interfaces except the service method.

GenericServlet may be directly extended by Servlet. It provides simple versions of the life cycle methods init( ) and destroy( ) methods.

GenericServlet Class Methods
Following are the important methods of GenericServlet Class:

public void destroy( )Invoked by servlet container. It shows that the servlet is being taken out of service.
public String getInitParameter(String name)Returns a String containing the value of named parameter.
public String getServletInfo( )Returns information related to Servlet like author, version etc.
public String getServletName( )Returns the name of Servlet object.
public void init( )It is a convenience method that can easily be overridden so that we do not need to call super.init(config).
public void log(String msg)Writes the given message to a Servlet log file.
public abstract void service(ServletRequest req, ServletResponse res)It is an abstract method, called by the servlet container to allow the servlet to respond to a request.