What is Servlet in Java

What is Servlet in Java: Life Cycle, Hello World Example with web.xml (2026)

What is Servlet in Java: Life Cycle, Hello World Example with web.xml

What is a Servlet?

  • A Servlet is a technology used to develop web applications.
  • A Servlet is an API written in the Java language.
  • A Servlet is server-side programming used to create dynamic pages.
  • In MVC (Model, View, Controller), a servlet works as the controller.
  • A Servlet is also a container that holds servlet instances.
  • A Servlet is based on configuration. You can use a mapping file or annotations to configure it.
  • A Servlet is a predefined interface that exists in the javax.servlet package.
  • A Servlet is server-side programming, which means you need a server to run servlet programs.
  • A Servlet is based on a life cycle that performs specific tasks.

There are three ways to create a Servlet:

  1. By implementing the Servlet interface
  2. By extending the GenericServlet class
  3. By extending the HttpServlet class

Project structure for a Servlet web application

Before writing a servlet program, you need to understand the project structure used to create a web application.

src: In this folder you keep Java files — model classes, servlet classes, database code, and so on.

WebContent: This folder contains web-related resources like HTML files, JSP files, images, CSS, and JavaScript. You can organize images in an images folder, CSS files in a css folder, and JavaScript files in a js folder.

WEB-INF: This folder holds the lib folder where you keep external JAR files. It also holds web.xml, the deployment descriptor where the servlet class is mapped. In Servlet 3.0 and later, you can remove web.xml and map everything with annotations.

Servlet interface life cycle methods

The Servlet interface has five methods: init(), service(), destroy(), getServletConfig(), and getServletInfo().

Hello World using the Servlet interface

In the src folder, create a package with any name. In this example, I am using com.deepsingh44.blogspot.controller. Then create a class HelloWorldServlet like below:

package com.deepsingh44.blogspot.controller;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloWorldServlet implements Servlet {

    private ServletConfig arg0;

    @Override
    public void destroy() {
        System.out.println("destroy here...");
    }

    @Override
    public ServletConfig getServletConfig() {
        return arg0;
    }

    @Override
    public String getServletInfo() {
        return "Hello World";
    }

    @Override
    public void init(ServletConfig arg0) throws ServletException {
        this.arg0 = arg0;
        System.out.println("initialized here...");
    }

    @Override
    public void service(ServletRequest arg0, ServletResponse arg1)
            throws ServletException, IOException {

        // Write to the server console
        System.out.println("serve to client from here");

        // Write plain text to the client's browser
        arg1.getWriter().print("Hello World in plain text on browser");

        // To write HTML to the browser, set the content type first
        arg1.setContentType("text/html");
        arg1.getWriter().print("<h1>Hello World in HTML on browser</h1>");
    }
}

Map the servlet in web.xml

After creating the servlet class, you need to map it in web.xml to create a URL you can access from the browser.

<web-app>

    <servlet>
        <servlet-name>firstdemo</servlet-name>
        <servlet-class>com.deepsingh44.blogspot.controller.HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>firstdemo</servlet-name>
        <url-pattern>/helloservlet</url-pattern>
    </servlet-mapping>

</web-app>

Run the application on Tomcat

Deploy the application to Tomcat and open the browser. If you request the wrong URL, you get a 404 page like this:

The 404 appears because you did not request the mapped URL. Call the URL that matches your url-pattern:

Now you get the successful result.

Where to go next

You now know what a Servlet is, how the project is structured, what the life cycle methods do, how to write a Hello World servlet by implementing the Servlet interface, and how to map it in web.xml.

If you remember one thing, remember this: a servlet is a Java class that runs on a server and produces a response to an HTTP request. The container calls init() once, service() for every request, and destroy() once before removal.

If you want to go deeper, try these next steps:

  • Rewrite the same servlet by extending HttpServlet instead of implementing the Servlet interface. Notice how much less code you write.
  • Replace web.xml with the @WebServlet("/helloservlet") annotation. Compare both approaches.
  • Read request parameters with request.getParameter("name") and echo them back to the browser.
  • Move on to JSP. It sits on top of servlets and lets you write HTML without Java clutter. Once you see how JSP compiles down to a servlet, the whole web stack clicks.

No comments: