Java Web Application

Prerequisites

JDK and Apache Maven

On Ubuntu 18.04, you can install maven and you will also get OpenJDK.

sudo apt-get update && sudo apt-get install maven -y

After installation is completed, check versions:

$ java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)

$ mvn --version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 11.0.8, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-17763-microsoft", arch: "amd64", family: "unix"

Or you can manually install from:

Configure JAVA_HOME

Find JDK home directory

$ java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
    java.home = /usr/lib/jvm/java-11-openjdk-amd64

Edit ~/.bashrc and append these lines:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Tomcat

Tomcat is required to run your WAR file. Download Apache Tomcat 8.5 from https://tomcat.apache.org/download-80.cgi

Extract files and append the following lines to ~/.bashrc.

CATALINA_HOME=$HOME/bin/apache-tomcat-8.5.57
PATH=$PATH:$HOME/bin/apache-tomcat-8.5.57/bin

Configure Tomcat

Insert the following lines between <tomcat-users></tomcat-users> block

  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="password" roles="manager-gui, manager-script"/>

Start and Stop Tomcat

Start Tomcat

startup.sh

Visit http://localhost:8080 to check server is up

Stop Tomcat

shutdown.sh

Initializing Project

Generate the project

mvn archetype:generate "-DgroupId=sh.par" "-DartifactId=helloworld" "-DarchetypeArtifactId=maven-archetype-webapp" "-Dversion=1.0-SNAPSHOT"

Build and package

mvn package

The WAR file in target folder.

Deploy to Tomcat

Start the server and go to web GUI at http://localhost:8080/ 

Click Manager App button

image-1598960550565.png

Input configured username and password.

Select the WAR file and click Deploy button

image-1598960649208.png

Your deployed application should appear on the list.

image-1598960721439.png

You can click Undeploy button to remove your application.

image-1598960766090.png

References


Revision #2
Created Tue, Sep 1, 2020 10:03 AM by Chairat (Par)
Updated Tue, Sep 1, 2020 11:47 AM by Chairat (Par)