Hello World Servlet starter

A small Java web project with one Servlet, one HTML form, and a Maven WAR build. Use JDK 21, Maven 3.9+ in the Maven 3 series, and Tomcat 10.1. JSP and database setup come later in the todo tutorial.

1. Open and build

Extract the ZIP. Open the hello-servlet folder containing pom.xml in VS Code. Open its terminal and run:

java -version
javac -version
mvn -version
mvn clean package

Wait for BUILD SUCCESS. Find target/hello-servlet.war. Building does not start Tomcat or deploy the WAR.

2. Deploy with File Explorer

  1. Copy target/hello-servlet.war into the running Tomcat's webapps folder. A Windows installer commonly uses C:\Program Files\Apache Software Foundation\Tomcat 10.1\webapps; use your actual installation.
  2. If Tomcat is running with default auto-deployment enabled, wait for deployment. Do not start another instance. If stopped, start the Apache Tomcat Windows service, or use catalina.bat run for an extracted installation. Choose one startup method.
  3. Open http://localhost:8080/hello-servlet/. If you configured the tutorial's port 8083, use http://localhost:8083/hello-servlet/ instead.

3. Check your first Servlet

MacBook deployment

Use macOS Terminal/Zsh, a matching JDK 21, and an extracted Tomcat 10.1. Replace the Tomcat folder below with your actual extracted version. Run from the hello-servlet folder, after stopping your own foreground Tomcat:

export JAVA_HOME="$(/usr/libexec/java_home -v 21)"
export CATALINA_HOME="$HOME/tools/apache-tomcat-10.1.61"
mvn -version
mvn clean package
cp target/hello-servlet.war "$CATALINA_HOME/webapps/hello-servlet.war"
bash "$CATALINA_HOME/bin/catalina.sh" run

Copy/start only after BUILD SUCCESS. If another Tomcat already runs, use that instance instead of starting a second copy. For persistent Path settings, Apple Silicon/Intel downloads and common Mac errors, open the complete MacBook setup guide.

Click the Hello World link. At /hello-servlet/hello, expect Hello, World!. Enter Priya in the form; expect Hello, Priya!. This response is generated by HelloServlet.doGet, rather than the HTML file.

4. Understand the files

If something fails

For 404, confirm hello-servlet.war is in the running instance's webapps folder, wait for deployment and inspect its logs. For an occupied port, use the existing server. In Windows CMD use copy, %CATALINA_HOME% and .bat scripts; Linux/macOS cp and .sh commands belong in Bash/Zsh.

Full installation, deployment and troubleshooting instructions

After changing code, rebuild the WAR and redeploy it. When this project works, continue with the larger todo-servlet-jsp project.