Servlet Example

Servlets are used to create dynamic web application in java.javax.servlet packages provides the interfaces and clases. They are not specific to any protocol. javax.servlet.http package contains interfaces and classes processes for http requests.

Servlet has following life cycle methods

  • init() : This method is invoked only once which and used for initialization of servlet.
  • service(): This method is invoked at each time while a request comes for the servlet.
  • destroy():This method is invoked only once and used to do clean up operations.

The HttpServlet class extends the GenericServlet class and implements Serializable interface.

Commonly used methods of HttpServlet

  • protected void doGet(HttpServletRequest req, HttpServletResponse res) :- It handles the GET request.
  • protected void doPost(HttpServletRequest req, HttpServletResponse res) :- It handles the POST request

Here we are taking simple example for HTTP servlet using Eclipse IDE.


Steps to create web application using Eclipse IDE.

  • Create a dynamic web application
  • Add servlet-api.jar and jsp-api.jar to the WEB-INF/lib.
  • Create a package under src directory
  • Add a Servlet to the package the package by right click on package and choose the New->Servlet.
  • Create a index.jsp under WebContent directory
  • Now finally create a web.xml file under WEB-INF directory and add the following entry to the servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Servlet Example</title>
<h1>Servlet Example by StartWithJava</h1>
<form action="HelloWorldServlet" method="post">
<input type="text" name="name">
<input type="submit" value="Submit">

package com.startwithjava;

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

* Servlet implementation class HelloWorldServlet
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//getWriter() method of HttpServletResponse is used to generate response
PrintWriter out = response.getWriter();
//getParameter() method of HttpServletRequest
String name = request.getParameter("name");
out.println("<b>Welcome "+name+"</b>");


Now deploy the web application on the server.Import war file using Eclipse IDE.You can access the application using following url.


Download War file.

Leave a Reply