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:

hello world servlet hello world servlet

 

Source code for the HelloWorld Servlet: https://github.com/fmarchioni/mastertheboss/tree/master/helloworld

Found the article helpful? if so please follow us on Socials