Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle

Overview

--Create a WAR file in Gradle containing Java Servlet and JSP --Deploy to Apache Tomcat 9

This environment

Create a WAR file with Gradle containing Java Servlet and JSP

Prepare a directory to put the 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 a WAR file

Create a WAR file with the 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 the installation directory

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

$ mkdir tomcat9

$ cd tomcat9

Download and unzip 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 booting

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.

Make sure you access it with curl and start it.

$ 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.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
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 Apache Tomcat 9 webapps directory

$ 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 Servlets and JSPs

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 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 material

Recommended Posts

Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Save files and folders using File Manager
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
How to download a file (Servlet, HTML, Apache, Tomcat)
Reasons to use Servlet and JSP separately in Java development
How to create a new Gradle + Java + Jar project in Intellij 2016.03
The story of forgetting to close a file in Java and failing
To create a Zip file while grouping database search results in Java
How to create a server executable JAR and WAR with Spring gradle
Try to create a bulletin board in Java
To manually deploy Struts2 as a war file
How to ZIP a JAVA CSV file and manage it in a Byte array
How to create a Java environment in just 3 seconds
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
How to convert a file to a byte array in Java
Gzip-compress byte array in Java and output to file
[Java] Create a temporary file
Create a jar file that can be executed in Gradle
How to deploy a simple Java Servlet app on Heroku
I want to create a Parquet file even in Ruby
How to develop and register a Sota app in Java
Create a Java and JavaScript team development environment (gradle environment construction)
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
SCP transfer War file to Tomcat
Create a memo app with Tomcat + JSP + Servlet + MySQL using Eclipse
[Java] How to create a folder
Create a Servlet program in Eclipse
Add a time stamp to the JAR file name in Gradle
Sample to read and write LibreOffice Calc fods file in Java 2021
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
Hello world in Java and Gradle
I want to create a Servlet war file with OpenJDK on CentOS7. Without mvn. With no internet connection.
Inexperienced create a weather app using OpenWeatherMap and deploy it to Netlify
How to test a private method in Java and partially mock that method
Link Apache and Tomcat in a blink of an eye on CentOS 8
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to record JFR (Java Flight Recorder) and output a dump file
Have a tool to create and open a new canvas in Mac preview
How to create your own annotation in Java and get the value
[Java] Create and apply a slide master
Java implementation to create and solve mazes
Deploy Java Servlet app locally on Tomcat
Create a TODO app in Java 7 Create Header
[Introduction to JSP + Servlet] A little animation ♬
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Let's create a TODO application in Java 9 Create TODO display Sort by date and time + Set due date default to today's date
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
Create a Windows desktop application in Ruby and distribute an executable file (.exe)!
[Java] [POI] Create a table in Word and start a new line in one cell
A memo to simply create a form using only HTML and CSS in Rails 6
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
Let's create a versatile file storage (?) Operation library by abstracting file storage / acquisition in Java
Scraping and writing specific elements to a file
Create barcodes and QR codes in Java PDF
Two ways to start a thread in Java + @
Create a CSR with extended information in Java
How to display a web page in Java
Code to escape a JSON string in Java
[Raspberry Pi] Try to link Apache2 and Tomcat