Simple "Hello World" Tomcat servlet for performance testing

Step 1: Set Up Your Project Structure

MyJsonServlet/
├── src/
│   └── com/
│       └── example/
│           └── JsonStatusServlet.java
├── web/
│   ├── WEB-INF/
│   │   └── web.xml
│   └── index.html (optional)
└── build.gradle or pom.xml (for dependency management)

Step 2: Create the Servlet Class

package com.example;

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("/status")  // This annotation maps the servlet to /status URL
public class JsonStatusServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // Set response content type to JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        
        // Create the JSON response
        String jsonResponse = "{\"status\": \"OK\", \"message\": \"Server is running\", \"timestamp\": " + 
                             System.currentTimeMillis() + "}";
        
        // Write the JSON response
        PrintWriter out = response.getWriter();
        out.print(jsonResponse);
        out.flush();
    }
}

Step 3: Configure web.xml (Alternative to Annotation)

If you're not using the @WebServlet annotation, create/modify WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    
    <servlet>
        <servlet-name>JsonStatusServlet</servlet-name>
        <servlet-class>com.example.JsonStatusServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>JsonStatusServlet</servlet-name>
        <url-pattern>/status</url-pattern>
    </servlet-mapping>
    
</web-app>

Step 4: Build and Deploy

Using Maven:

1. Add this to your pom.xml:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

2. Build with mvn package and deploy the WAR file to Tomcat's webapps directory

Manually:

1. Compile the servlet:

javac -cp "/path/to/tomcat/lib/servlet-api.jar" src/com/example/JsonStatusServlet.java -d WEB-INF/classes

2. Create WAR file:

jar cvf MyJsonServlet.war *

3. Copy the WAR file to Tomcat's webapps directory

Step 5: Test the Servlet

Start Tomcat and visit:

http://localhost:8080/MyJsonServlet/status

You should see the JSON response:

{
    "status": "OK",
    "message": "Server is running",
    "timestamp": 1620000000000
}