HelloWorld from Java jsp with OpenJDK 11 packaged Maven war and deploy on Tomcat 9 - jbilander/HowTos GitHub Wiki

HelloWorld from Java jsp with OpenJDK 11 packaged Maven war and deploy on Tomcat 9

Download latest OpenJDK 11 binary distribution from adoptopenjdk.net, I will use Windows here:

Unzip to your prefered destination, e.g.

C:\Program Files\Java

It should now look like this:

Directory of C:\Program Files\Java\jdk-11.0.1+13
2018-11-09  16:31    <DIR>          bin
2018-11-09  16:30    <DIR>          conf
2018-11-09  16:30    <DIR>          demo
2018-11-09  16:30    <DIR>          include
2018-11-09  16:30    <DIR>          jmods
2018-11-09  16:30    <DIR>          legal
2018-11-09  16:30    <DIR>          lib
2018-11-09  16:30             1 230 release

Download, install and start latest IntelliJ IDEA Community Edition, version 2018.3.3, at the time of writing.

Create a new project, we'll call it HelloWorld, make sure the JDK 11 is selected as the Project SDK:

Right click the project folder and Add framework support... -> Maven

This creates a pom.xml file, put these lines in the file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <warName>hellofromjava##${project.artifactId}</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Click Import Changes:

Now create a folder webapp under the folder src/main by right clicking the main folder:

Create an index.jsp in the webapp folder and add these lines:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.Date" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <h1><% out.print("Hello World!"); %></h1>
    Today is: <% out.print(new Date().toString()); %>
</body>
</html>

Click Maven tab and update with Reimport All Maven Projects

Build the war file by double clicking package

Maven build console output:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloWorld ---
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloWorld ---
[INFO] skip non existing resourceDirectory C:\Projects\HelloWorld\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ HelloWorld ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloWorld ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ HelloWorld ---
[INFO] Packaging webapp
[INFO] Assembling webapp [HelloWorld] in [C:\Projects\HelloWorld\target\HelloWorld-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Projects\HelloWorld\src\main\webapp]
[INFO] Webapp assembled in [83 msecs]
[INFO] Building war: C:\Projects\HelloWorld\target\hellofromjava##HelloWorld.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.398 s
[INFO] Finished at: 2019-01-19T22:13:26+01:00
[INFO] Final Memory: 12M/44M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

A war file with the name hellofromjava##HelloWorld.war is created. The context path will be /hellofromjava when deployed on tomcat.

https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming

Copy the war file over to the server with WinSCP or similar scp client:

ssh into the server and change ownership of the file:

root@zeus:~# chown tomcat:tomcat hellofromjava##HelloWorld.war

Move the file to the webapps folder to get autodeployed:

root@zeus:~# mv hellofromjava##HelloWorld.war /opt/tomcat/webapps/

Check that it got deployed:

root@zeus:~# cd /opt/tomcat/webapps/

root@zeus:/opt/tomcat/webapps# ls -al
total 39
drwxr-x--- 5 tomcat tomcat    6 Jan 19 21:12 .
drwxr-xr-x 9 root   tomcat   16 Jan 17 19:35 ..
drwxr-x--- 4 tomcat tomcat    4 Jan 19 22:19 hellofromjava##HelloWorld
-rw-rw-r-- 1 tomcat tomcat 1677 Jan 19 22:19 hellofromjava##HelloWorld.war
drwxr-x--- 5 tomcat tomcat    7 Jan 17 19:35 host-manager
drwxr-x--- 5 tomcat tomcat    8 Jan 17 19:35 manager

root@zeus:/opt/tomcat/webapps# cd ..
root@zeus:/opt/tomcat# cd logs/
root@zeus:/opt/tomcat/logs# tail -20 catalina.out
...
19-Jan-2019 22:19:27.625 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/apache-tomcat-9.0.14/webapps/hellofromjava##HelloWorld.war]
19-Jan-2019 22:19:27.655 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/opt/apache-tomcat-9.0.14/webapps/hellofromjava##HelloWorld.war] has finished in [30] ms

Now, set up the ProxyPass directive in Apache

root@zeus:/opt/tomcat/logs# vi /etc/apache2/sites-enabled/vhosts-default.conf

<VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /var/www/example
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        <Directory /var/www/example>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>
        ProxyPass "/hellofromjava" "ajp://127.0.0.1:8009/hellofromjava"
</VirtualHost>

Reload the config file:

root@zeus:/opt/tomcat/logs# systemctl reload apache2

Point your browser to http://www.example.com/hellofromjava

Nice, works just fine

To Undeploy just delete the war file:

root@zeus:/opt/tomcat/webapps# rm hellofromjava##HelloWorld.war

That's all folks!

⚠️ **GitHub.com Fallback** ⚠️