Servlet Tutorial

Servlet tutorial provides basic and advanced ideas of CoreJava and Html. Our Servlet tutorial is designed for beginners and professionals.

Servlet technology is used to create web application (resides at server side and generates dynamic web page).

A Java servlet is a Java program that broadens the capacities of a server. Despite the fact that servlets can react to any sorts of solicitations, they most normally execute applications facilitated on Web servers. Such Web servlets are the Java partner to other element Web content technologies , for example, PHP and ASP.NET.


Before learning Servlet, you must have the fundamental information of Core Java.


Our Servlet tutorial is designed to help beginners and professionals.


We assure that you will not find any mistake in this Servlet tutorial. But if there is any mistake, please post the problem by mail.

In this page , you will learn important line
  • What is a servlet?
  • What is CGI?
  • What are the limitations of CGI?
  • What is the difference between Get and Post?

The term Servlet has two different meaning in two different contexts. In the broader context, it represents an API of dynamic web application development and in the narrow context, it represent a Java class which is defined using this API for processing requests in a web application.

As API, Servlet contains interfaces and classes which are used to create web applications in Java. Implementation of some of the these interfaces is provided by the web server vendors and implementation of the rest is provided by the application programmers. These interfaces define a standard mode of communication between the server and the Servlets (request processing programs). To understand the need of such interfaces, we need to understand how a request for dynamic contents is processed by the server.

A request for dynamic contents has following work flow:

  • The server intercepts the request and identifies the program to process it.
  • The server invokes the program and provides the request data to it.
  • The program processes the request and generates HTML contents.
  • These contents are provided to the server by the program.
  • The server sends these contents as response to the client.

i.e. A web server and a request processing program need to collaborate with each other in the processing of a request. For this collaboration there must be some standard rules of communication b/w the server and the program. Before Servlet these rules were provided by CGI. You must have heard this term before? No! don't worry, lets find out what it is?

The basic of servlet is to develop web applications. Servlets specification developed by SUN and released to the industry. Lot of server vendors came forward for implementing these specifications of servlets.

Servlets are nothing but set of rules given by SUN in the form of Interfaces. The server vendors have developed their own classes by implementing the interfaces developed by SUN.

What is Servlet ?

A servlet is a server side platform independent, dynamic and multithread java program, which runs in the context of server for extending the functionality of server. When multiple users make a request to the same servlet then all requests will be processed by the container by creating multiple threads for the same servlet.

Common Gateway Interface (CGI)

Common Gateway Interface is a protocol which provides standard rules for servers to invoke programs, to provide request data to them, and to receive processing result from them. In CGI based web applications programs were written in C, C++ or perl. These programs were called CGI Scripts. you would be thinking that when we had CGI, why servlet was introduced?

CGI based web applications had following drawbacks:

  1. For each request, a procees is started by the server to execute the CGI script. Process creation and destruction, used to result in more overhead than actual request processing.
  2. CGI Scripts were platform dependent i.e. if a web application is to be deployed from Windows to Linux server or vice-versa, CGI scripts need to be recompiled for the target platform.

To remove these drawbacks of CGI based web applications, Sevlet API was introduced by sun Microsystems in 1998. Now, you will be wondering, how Servlets removes these drawbacks?

  1. To remove the overhead of process creation & destruction, a thread based request processing model is provided by servlet.
  2. By facilitating the development of web applications in Java, problem of platform dependency is removed.

The role of the API, is to facilitate communication between a web server and a request processing program (henceforth called Servlet). For this Sun Microsystems defined a set of interfaces, representing standard rules for defining Servlets, instantiating Servlets, processing requests, and transferring data between the server and the servlets etc.

javax.servlet.Servlet is the main interface of the API, it provides methods which define initialization, processing and destruction phase of a Servlet. These methods are called servlet life cycle methods.

Application programmers are told by Sun Microsystems that they need to define classes which implements this interface i.e. application programmer are asked to define methods which are expected by the server.

A web server is told that application programmers will provide Sevlet classes to it as part of an application i.e. the server will need to invoke these methods for request processing.

By providing same information to both the parties Sun Microsystems facilitated communication between the web servers and servlets in a standard manner.

The basic terminology used in servlet.

  • HTTP
  • HTTP Request Types
  • Difference between Get and Post method
  • Container
  • Server and Difference between web server and application server
  • Content Type
  • Introduction of XML
  • Deployment

HTTP-Hyper Text Transfer Protocol

  • Http is the protocol that allows web servers and browsers to exchange data over the web.
  • It is a request response protocol.

The Http Request Methods

HTTP Request Description
GET Asks to get the resource at the requested URL.
POST Asks the server to accept the body info attached. It is like GET request with extra info sent with the request.
HEADstrong Asks for only the header part of whatever a GET would return. Just like GET but with no body.
TRACE Asks for the loopback of the request message, for testing or troubleshooting.
PUT Says to put the enclosed info (the body) at the requested URL.
DELETE Says to delete the resource at the requested URL.
OPTIONS Asks for a list of the HTTP methods to which the thing at the request URL can respond

What is the difference between Get and Post?

Have you ever thought how a request is mapped to doGet() and doPost() methods that we define in our Servlet classes. The Servlet interface which defines the life cycle of servlets provides service() method for request processing.

The server doesn't know doGet() and doPost() methods. It simply invokes service() method whenever a request is received for a servlet. The mapping of service() to doGet() and doPost() is done by HttpServlet class. You may recall that GenericServlet class doesn't define service() method and is extended by HttpServlet class. We extend HttpServlet but don't define service() method and doesn't declare out classes as abstract which implies that the service() method is defined by HttpServlet class.

Lets find out how a request is mapped to doGet() and doPost() methods.

Actually the HttpSevlet class has two service() methods, one protocol independent that is known to the server and one protocol specific which is not known to the server. These methods have following signature:

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;

protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException;

There are many differences between the Get and Post request.

1) In case of Get request, only limited amount of data can be sent because data is sent in header. In case of post request, large amount of data can be sent because data is sent in body.
2) Get request is not secured because data is exposed in URL bar. Post request is secured because data is not exposed in URL bar.
3) Get request can be bookmarked Post request cannot be bookmarked
4) Get request is idempotent. It means second request will be ignored until response of first request is delivered. Post request is non-idempotent
5) Get request is more efficient and used more than Post Post request is less efficient and used less than get.

Content Types

What is a Content-Type?

A "Content-type" is simply a header defined in many protocols, such as HTTP, that makes use of MIME types to specify the nature of the file currently being handled.

What is a MIME type?

MIME stands for "Multipurpose Internet Mail Extensions. It's a way of identifying files on the Internet according to their nature and format. For example, using the "Content-type" header value defined in a HTTP response, the browser can open the file with the proper extension/plugin.

How can I set the Content-type header of my files?

It depends on the programming language you are using. Note that most language have a default "Content-type" of "text/html".

    Setting the Content-type in Java:
  • // Response is of type javax.servlet.ServletReponse
  • response.setContentType("text/plain");>