Java Servlet

last modified January 27, 2024

Java Servlet tutorial shows how to create a simple servlet in Java. We use embedded Jetty server.

Java Servlet

Servlet is a Java class which responds to a network request. This is mostly an HTTP request. Java servlets are used to create web applications. They run in servlet containers such as Tomcat or Jetty. Modern-day Java web development uses frameworks that are built on top of servlets. For instance, Spring or Vaadin frameworks use servlets.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.

Java Servlet example

In the following example, we use the @WebServlet annotation to create a Java Servlet. Alternatively, the mapping can be created in the web.xml file.

│   ├───java
│   │   └───com
│   │       └───zetcode
│   │               HelloServlet.java
│   ├───resources
│   └───webapp
│           index.html

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"










This is the Maven POM file. The jakarta.servlet-api provides the Servlet API. The provided scope makes the dependency available at compile time and indicates that it is already available at runtime. In is included with the Servlet container such as Jetty.

The maven-war-plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive. The jetty-maven-plugin allows us to run embedded Jetty server with mvn jetty:run.


In the Jetty Maven plugin, we set the context path to /app.

package com.zetcode;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "HelloServlet", urlPatterns = {"/hello"})
public class HelloServlet extends HttpServlet {

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

        var out = response.getOutputStream();

        out.print("Hello there from Servlet");

The HelloServlet returns a simple text message back to the client.

@WebServlet(name = "HelloServlet", urlPatterns = {"/hello"})

The Java class is decorated with the @WebServlet annotation. It is mapped to the hello URL pattern.

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

The doGet method is called for GET requests. The method receives HttpServletRequest and HttpServletResponse objects.


The servlet sends output data in plain text and the encoding of the data is set to UTF-8.

var out = response.getOutputStream();

With the getOutputStream method, we get the servlet output stream. Note that we do not close the output stream; this is a task for the container.

out.print("Hello there from Servlet");

We write a text message with the print method.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Home page</title>

    This is home page. Call <a href="/app/hello">HelloServlet</a>


In the home page, we have a link that calls the servlet.

$ mvn jetty:run

We run the embedded Jetty server and navigate the browser to http://localhost:8080/app/.


Jakarta Servlet documentation

In this article we have shown how to create a simple Java Servlet with embedded Jetty server.


My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Java tutorials.