[Java] Gradle Java Servlet and JSP WAR files to deploy on Apache Tomcat 9

3 minute read


  • Create a WAR file with Java Servlet and JSP with Gradle
  • Deploy to Apache Tomcat 9

This environment

  • Apache Tomcat 9.0.37 (Java Servlet 4.0, JSP 2.3)
  • Java 14 (AdoptOpenJDK 14.0.1)
  • Gradle 6.5.1
  • macOS 10.15 Catalina

Create a WAR file including Java Servlet and JSP with Gradle

Prepare a directory for source code

This time, create a directory called mywebapp in any directory and prepare a simple sample code in it.

$ mkdir mywebapp

$ cd mywebapp

File list

├── build.gradle
└── src
    └── main
        ├── java
        │  └── com
        │  └── example
        │  └── MyServlet.java
        └── webapp
            ├── WEB-INF
            │  └── web.xml
            └── myjsp.jsp


plugins {

repositories {

dependencies {
  // Java Servlet 4.0 API
  // https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api

// Java 14
sourceCompatibility = 14
targetCompatibility = 14

// Application
version = '1.0'


package com.example;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
    res.setContentType("text/html; charset=utf-8");
    try (PrintWriter out = res.getWriter()) {
      out.println("Servlet: Hello Servlet World!<br>");


<%@ page contentType="text/html; charset=utf-8" %><html><body>
JSP: Hello JSP World!<br>
<%= pageContext.getServletContext().getServerInfo() %><br>
java.vm.name: <%= System.getProperty("java.vm.name") %><br>
java.vm.vendor: <%= System.getProperty("java.vm.vendor") %><br>
java.vm.version: <%= System.getProperty("java.vm.version") %><br>


<?xml version="1.0" encoding="UTF-8"?>

<!-- Web Application Deployment Descriptor (Java Servlet 4.0) -->




Create WAR file

Create WAR file with Gradle build task.

$ gradle build

Make sure the WAR file is generated.

$ file build/libs/mywebapp-1.0.war
build/libs/mywebapp-1.0.war: Zip archive data, at least v1.0 to extract

Install Apache Tomcat 9

Prepare a directory to install

This time, create a directory called tomcat9 in any directory and install it in it.

$ mkdir tomcat9

$ cd tomcat9

Download and extract Apache Tomcat 9

Apache Tomcat ® - Apache Tomcat 9 Software Downloads

$ wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.tar.gz

$ tar xf apache-tomcat-9.0.37.tar.gz

Prepare a Java VM for startup

Set the environment variable JAVA_HOME.

$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home


$ java -version
openjdk version "14.0.1" 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)

Make sure Apache Tomcat 9 starts

Start Apache Tomcat 9 with startup.sh.

$ ./apache-tomcat-9.0.37/bin/startup.sh
Using CATALINA_BASE: /Users/foo/tomcat9/apache-tomcat-9.0.37
Using CATALINA_HOME: /Users/foo/tomcat9/apache-tomcat-9.0.37
Using CATALINA_TMPDIR: /Users/foo/tomcat9/apache-tomcat-9.0.37/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
Using CLASSPATH: /Users/foo/tomcat9/apache-tomcat-9.0.37/bin/bootstrap.jar:/Users/foo/tomcat9/apache-tomcat-9.0.37/bin/tomcat-juli.jar
Tomcat started.

Confirm that it is started by accessing with curl.

$ curl --include -s http://localhost:8080/ | head -15
HTTP/1.1 200
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 12 Jul 2020 10:38:03 GMT

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.37</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />

Shut down Apache Tomcat 9 with shutdown.sh.

$ ./apache-tomcat-9.0.37/bin/shutdown.shUsing CATALINA_BASE: /Users/foo/tomcat9/apache-tomcat-9.0.37
Using CATALINA_HOME: /Users/foo/tomcat9/apache-tomcat-9.0.37
Using CATALINA_TMPDIR: /Users/foo/tomcat9/apache-tomcat-9.0.37/temp
Using JRE_HOME: /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
Using CLASSPATH: /Users/foo/tomcat9/apache-tomcat-9.0.37/bin/bootstrap.jar:/Users/foo/tomcat9/apache-tomcat-9.0.37/bin/tomcat-juli.jar
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/ sun.rmi.transport=ALL-UNNAMED

Deploy the WAR file to Apache Tomcat 9

Place the WAR file in the webapps directory of Apache Tomcat 9

$ cp mywebapp/build/libs/mywebapp-1.0.war tomcat9/apache-tomcat-9.0.37/webapps/mywebapp.war

Start Apache Tomcat 9

$ ./tomcat9/apache-tomcat-9.0.37/bin/startup.sh

Access Java Servlet and JSP

Access Java Servlet with curl and check the operation.

$ curl --include http://localhost:8080/mywebapp/myservlet
HTTP/1.1 200
Content-Type: text/html;charset=utf-8
Content-Length: 94
Date: Sun, 12 Jul 2020 13:06:56 GMT

Servlet: Hello Servlet World!<br>
Apache Tomcat/9.0.37

Access the JSP with curl and check the operation.

$ curl --include http://localhost:8080/mywebapp/myjsp.jsp
HTTP/1.1 200
Set-Cookie: JSESSIONID=EE7C8309A86FBBEDBCEB618759FF6022; Path=/mywebapp; HttpOnly
Content-Type: text/html;charset=utf-8
Content-Length: 204
Date: Sun, 12 Jul 2020 13:07:08 GMT

JSP: Hello JSP World!<br>
Apache Tomcat/9.0.37<br>
java.vm.name: OpenJDK 64-Bit Server VM<br>
java.vm.vendor: AdoptOpenJDK<br>
java.vm.version: 14.0.1+7<br>

Reference materials