Java Servlet

last modified July 6, 2020

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 javax.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.

package com.zetcode;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.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="/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 localhost:8080.

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

List all Java tutorials.