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 You have created a Maven project. For more information, see Creating a Maven project for a Hello World application . Procedure 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. Navigate to the <application_home> directory. Create a directory to store the Java files. Navigate to the new directory. 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 . 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. Update the "index.jsp" file to redirect to the Hello World servlet by replacing its content with the following content: Navigate to the <application_home> directory. Compile and package the application as a web archive (WAR) with the following command: Example output steps Deploying an application to the server

Commands:
<project> <dependencies> <dependency> 1 <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <scope>provided</scope> 2 </dependency> </dependencies>
mkdir -p src/main/java/org/jboss/as/quickstarts/helloworld
cd src/main/java/org/jboss/as/quickstarts/helloworld
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(); } }
<html> <head> <meta http-equiv="Refresh" content="0; URL=HelloWorld"> </head> </html>
mvn package
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------