A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

Servlet Life Cycle

The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps.

HTTP methods — doGet and doPost

  1. Let’s extend our Servlet class to HttpServlet. This extension allows us to use the doGet method and doPost. Get and Post are common methods of the HTTP to indicate the kind of action that we want. Here are definition of some of the methods commonly used:
  • GET — Fetches data from server
  • POST — Sends data to server
  • PUT — Replaces current representation of target resource with uploaded content
package com.hello;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void init() throws ServletException {
// Servlet initialization code here

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

// Set response content type

// Actual logic goes here.
PrintWriter out = response.getWriter();

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {


public void destroy() {
// resource release

Deployment Description (web.xml)

  1. In the index.html form, when we click on submit button and ask for /hello path, we really want to call HelloServlet but our application does not know to do that. Hence we need to link both via deployment description of web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
  1. sendRedirect
  1. Use request.setAttribute to pass data using RequestDispatcher and perform url rewriting using sendRedirect .

JavaServer Pages (JSP)

We can see to build a form in Server, we have to wrap each line of the HTML with PrintWriter but we have to work allot. We can further improve this by using JSP.

  • Reduction in the length of Code :- In JSP we use action tags, custom tags etc.
  • Connection to Database is easier :-It is easier to connect website to database and allows to read or write data easily to the database.
  • Make Interactive websites :- In this we can create dynamic web pages which helps user to interact in real time environment.
  • Portable, Powerful, flexible and easy to maintain :- as these are browser and server independent.
  • No Redeployment and No Re-Compilation :- It is dynamic, secure and platform independent so no need to re-compilation.
  • Extension to Servlet :- as it has all features of servlets, implicit objects and custom tags
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<title>JSP - Hello World</title>
String hello = "Hello";
<h1><%= hello %></h1>
<%! Dec variable %>
<%! int variable = 10; %>
<% java code %>
<%= expression %>
<% num1 = num1+num2 %>
<h1><%= expression %></h1>
<% -- Comments %>

Full Stack Web Application and Software Developer