Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
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.xmlconfiguration 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-apidependency provides Jakarta Servlet API.- 2
- Define the scope as
providedso 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 thejboss-eap-ee-with-toolsBOM and such dependencies are are included with JBoss EAP.
NoteThe dependency is defined without a version because
jboss-eap-ee-with-toolsBOM was imported in the<dependencyManagement>section.- Navigate to the <application_home> directory.
Create a directory to store the Java files.
$ mkdir -p src/main/java/org/jboss/as/quickstarts/helloworldNavigate to the new directory.
$ cd src/main/java/org/jboss/as/quickstarts/helloworldCreate the Servlet
HelloWorldServlet.javathat 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:
<html> <head> <meta http-equiv="Refresh" content="0; URL=HelloWorld"> </head> </html>- Navigate to the <application_home> directory.
Compile and package the application as a web archive (WAR) with the following command:
$ mvn packageExample output
... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...