Chapter 2. Creating a hello world servlet


Create a servlet that returns "Hello world!" when accessed.

In this procedure, <application_home> refers to the directory that contains the pom.xml configuration file for the application.

Prerequisites

Procedure

  1. Add the required dependency to pom.xml configuration file after the <dependencyManagement> section.

    <project>
        ...
        <dependencies>
            <dependency>                                             
    1
    
                <groupId>jakarta.servlet</groupId>
                <artifactId>jakarta.servlet-api</artifactId>
                <scope>provided</scope>                              
    2
    
            </dependency>
        </dependencies>
    1
    jakarta.servlet-api dependency provides Jakarta Servlet API.
    2
    Define the scope as provided so that the dependency is not included in the application. The reason for not including the dependency in the application is that this dependency is managed by the jboss-eap-ee-with-tools BOM and such dependencies are are included with JBoss EAP.
    Note

    The dependency is defined without a version because jboss-eap-ee-with-tools BOM was imported in the <dependencyManagement> section.

  2. Navigate to the <application_home> directory.
  3. Create a directory to store the Java files.

    $ mkdir -p src/main/java/org/jboss/as/quickstarts/helloworld
  4. Navigate to the new directory.

    $ cd src/main/java/org/jboss/as/quickstarts/helloworld
  5. Create the Servlet HelloWorldServlet.java that returns "Hello World!".

    package org.jboss.as.quickstarts.helloworld;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import jakarta.servlet.ServletException;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    
    @WebServlet("/HelloWorld")                      
    1
    
    public class HelloWorldServlet extends HttpServlet {
    
        static String PAGE_HEADER = "<html><head><title>helloworld</title></head><body>";
    
        static String PAGE_FOOTER = "</body></html>";
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html");
            PrintWriter writer = resp.getWriter();
            writer.println(PAGE_HEADER);
            writer.println("<h1> Hello World! </h1>");
            writer.println(PAGE_FOOTER);
            writer.close();
        }
    }
    1
    The @WebServlet("/HelloWorld") annotation provides the following information to JBoss EAP:
    • This class is a servlet.
    • Make the servlet available at the URL "<application_URL>/HelloWorld".

      For example, if JBoss EAP is running on the localhost and is available at the default HTTP port, 8080, the URL is http://localhost:8080/helloworld/HelloWorld.

  6. Navigate to the <application_home>/src/main/webapp directory.

    You find the file "index.jsp" that Maven created. This file prints "Hello World!" when you access the application.

  7. Update the "index.jsp" file to redirect to the Hello World servlet by replacing its content with the following content:

    <html>
        <head>
            <meta http-equiv="Refresh" content="0; URL=HelloWorld">
        </head>
    </html>
  8. Navigate to the <application_home> directory.
  9. Compile and package the application as a web archive (WAR) with the following command:

    $ mvn package

    Example output

    ...
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    ...

Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2026 Red Hat
Back to top