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

3 minute read

Overview

  • 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

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

build.gradle

plugins {
  id'war'
}

repositories {
  mavenCentral()
}

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

// Java 14
sourceCompatibility = 14
targetCompatibility = 14

// Application
version = '1.0'

src/main/java/com/example/MyServlet.java

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;

@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {

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

src/main/webapp/myjsp.jsp

<%@ 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>
</body></html>

src/main/webapp/WEB-INF/web.xml

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

<!-- Web Application Deployment Descriptor (Java Servlet 4.0) -->
<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>myjsp</servlet-name>
    <jsp-file>/myjsp.jsp</jsp-file>
  </servlet>

  <servlet-mapping>
    <servlet-name>myjsp</servlet-name>
    <url-pattern>/myjsp</url-pattern>
  </servlet-mapping>

</web-app>

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

$PATH=${JAVA_HOME}/bin:${PATH}

$ 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">
    <head>
        <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

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

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

<html><body>
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>
</body></html>

Reference materials