HelloWorld Servlet example
In this tutorial we will learn how to code and deploy an HelloWorld Servlet on the latest version of WildFly.
A Java Servlet is a Java object that responds to HTTP requests while running inside a Web container. In terms of process, the browser sends an HTTP request to the Java web server. The web server checks if a Servlet is registered for that request. If so, the Servlet is activated by calling the Servlet’s service() method.
Once the Servlet has been activated, it processes the request and generates a response. The response is then sent back to the browser.
The easiest way to implement this interface is to extend either the class javax.servlet.GenericServlet or javax.servlet.http.HttpServlet. If you are going to extend the GenericServlet class you will have to override the service() method that will be invoked when a request is received:
public class SimpleServlet extends GenericServlet {
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// do something in here
}
}
On the other hand, by extending HttpServlet you can implement some HTTP specific methods like doGet and doPost, which let you intercept an HTTP GET request and an HTTP POST request. See the following example:
package com.mastertheboss.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "hello", urlPatterns = { "/hello" })
public class HelloWorldServlet extends HttpServlet {
public HelloWorldServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<h1>Hello World Servlet on WildFly</h1>");
writer.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
In order to compile the above example, include the following dependency in your pom.xml file:
<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_4.0_spec</artifactId>
<scope>provided</scope>
</dependency>
On the other hand, if you want to compile using a “vanilla” Jakarta EE approach:
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Once deployed, you can reach the servlet from the browser:

Source code for the HelloWorld Servlet: https://github.com/fmarchioni/mastertheboss/tree/master/helloworld
Recommended Articles
Optimize Web Browsing Experience with Server Push on WildFly 31 Servlet 4.0
Learn how to use Server Push in Servlet 4.0 on WildFly 31 to improve perceived performance and reduce load times for web pages.
Mastering Undertow's Default Servlet for Static Resources and Directory Listing
Learn how to configure Undertow's Default Servlet for serving static resources and managing directory listings. #Java #Middleware #CloudNative
Create a Resource Adapter for WildFly 10 with Java Connector Architecture (JCA) | JCA Demo Tutorial
Learn how to create and deploy a HelloWorld Resource Adapter on WildFly 10 using the Java Connector Architecture (JCA). Follow this tutorial to create a new Maven project, define your connector exposed methods, and implement the Managed Connection class.
Upgrade WildFly 8.0.0 to 8.1.0 with a Single Patch: A Step-by-Step Guide
Learn how to apply a patch to upgrade your WildFly 8.0.0 application server to the latest 8.1.0 version in this step-by-step tutorial.