Quote
Hi @all, itβs me again, long time no see, how is your new year? Letβs kickoff a bit energy with simple trying from my site with new technology and methodology to build and serve Java API with couple of tools, such as Maven, Tomcat and see how it work. Now, letβs digest for first blog of 2025
Java world and attachable technologies
Question
When I come up with new idea 2025, I think about learn around webserver for first start and luckily, I found to
Tomcat
cuz I have request from my old co-worker to help him see why his web hosted byTomcat
is not work, I give a bit time to see Tomcat work and It actually kinda interesting
But anyway, when I try to reach to Tomcat and I know this tool spent for Java, especially Java Servlet because I know this technology for the long time before when I learn that one in university, kinda old but yeah, I found some a bit idea to foster that become more enchanting.
Java
and C#
are both of big technology, I means there are a lot of differences to build up the product from those languages, you need to know about
- Build tools
- WebServer for serving
- Integrate attachable
- and more and more
That why make those language is becoming big and separate with DevOps who work with lot of stuff related Node, PHP, β¦ But corresponded among languages, we always have multiple choice about technology for each topics above, and java is really insane. Letβs see what we got
Following LambdaTest - 19 Best Java Build Tools For Developers and BairesDevBlog - Top 5 Java Build Tools Compared about Build Tools for Java, you often see couple of things familiar, including
- Apache Ant: A Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other (XML-based configurations)
- Apache Maven (Recommendation): An open-source project management and build automation tool sed mainly forΒ Java projects (Declarative formats)
- Gradle: An advanced build automation tool thanks to its powerful, flexible domain-specific language based on Groovy/Kotlin
- Others way, we have CMake, Bazel, β¦ but that not spend for all of us
Quote
When I hear from my friend, shout out to Tri An who super recommendation about Maven and talk Maven is most of powerful thing him work with as Java Developer Engineer
Next, I spend a couple mins and see about list of WebServer for Java Developed and It provides a lot of information about which one should be choice, explore at Baeldung - Web and Application Servers for Java
- Apache Tomcat: One of the more popular web servers in the Java ecosystem
- Jetty: A highly scalable and memory-efficient web server and servlet container, supporting many protocols such as HTTP/3,2,1 and WebSocket
- Apache TomEE: A lightweight, yet powerful, JavaEE Application server with feature rich tooling.
- Oracle WebLogic Server: Β A unified and extensible platform for developing, deploying, and running enterprise applications in Java for on-premises and in the cloud
Quote
Yeah, absolutely multiple optional for newbie when they want to start like me, so I ask my friend, again Tri An and him said about only Maven or should be attached with βTomcatβ cuz Jetty is not good enough when compare between them.
Spend a bit time and I see couple of blogs write about e2e pipeline CI/CD with Java and he try to combine between Tomcat and Maven, so I catch this chance to setup and see how both them work π. BTW, you can explore those blogs below
- Medium - Real Time CI/CD Pipeline for Java Application to Deploy on Apache Server
- Medium - π»10 Stages Real-World CI/CD DevSecOps Pipeline for Deployment of Petclinic Application π»
- Medium - Jenkins CICD for Java Application on Tomcat
But first of all, let learn a bit about Maven and Tomcat
About Maven and Tomcat
Info
As usual, when kick off something like practice or lab session, I will spend a little bit time to go through technology used and see what is it ?
Maven
Info
What is Maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a projectβs build, reporting and documentation from a central piece of information.
Couple of primary goals of Maven, such as
- Making the build process easy
- Providing a uniform build system
- Providing quality project information
- Encouraging better development practices
Once information from documentation, βMaven is truly extend and reusable Ant scriptletsβ. It seems kinda straightforward contribute, but I know that do such incredible thing cuz that truthful have high recognize from community, especially Java Developer, so happiness and totally appreciate for such incredible contributing π
When you work with Maven
, You will spend a bit time to learn and define configuration through XML below couple of files, including
- POM (Project Object Model): An XML representation of a Maven project held in a file namedΒ
pom.xml
(Same aspackage.json
(Node) orrequirements.txt
(Python)) - Settings:
settings
Β element in theΒsettings.xml
Β file contains elements used to define values which configure Maven execution in various ways, like theΒpom.xml
, but should not be bundled to any specific project, or distributed to an audience. These include values such as the local repository location, alternate remote repository servers, and authentication information.
It actually complex than what I know π, BTW we will turn back when we start a practical session, but letβs see are there any thing else. So, I found about Maven Repository, again that kinda same like npm, pypi, crates, and moreover. It seems like place where developer public package, library to serve developing process.
To run and execute Maven
, this tool provides CLI, plugins, extension and you can try to install Maven
in your shell through some links, including
- Downloading Apache Maven 3.9.9
- Installing Apache Maven
- Running Apache Maven
- Maven in 5 Minutes
- Configuring Apache Maven
Success
I think kinda enough for
Maven
, if i spend hour inMaven
, I will share multiple information about this one and really confuse. Therefore, we will move toTomcat
before we turn back in the practical session
Tomcat
Info
What is Tomcat ?
Apache Tomcat is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a βpure Javaβ HTTP web server environment in which Java code can also run. Thus it is a Java web application server, although not a full JEE application server.
Following the Wikipedia - Apache Tomcat, they talk about these components implement and provide by Tomcat, such as
- Catalina: TomcatβsΒ servlet container. Catalina implementsΒ Sun Microsystemsβ specifications forΒ servletΒ and JavaServer Pages (JSP).
- Coyote: A Connector component for Tomcat that supports the HTTP 1.1 and 2 protocol as a web server
- Jasper: Tomcatβs JSP Engine. JasperΒ parsesΒ JSPΒ files to compile them into Java code as servlets (that can be handled by Catalina).
- Cluster: Β Used forΒ load balancingΒ that can be achieved through many techniques
- High availability: Facilitate the scheduling of system upgrades (e.g. new releases, change requests) without affecting the live environment.
- Plus more - Valve: Pluggable components for intercepting requests at any point in the processing pipeline.
Thank to Jimin who contribute legit helpful articles about Tomcat Architecture and Configuration with detailed explanation at
- Medium - Apache Tomcat (1): Core Components and Their Interactions
- Medium - Apache Tomcat (2): Navigating the Tomcat Configuration
From those articles, I collect some cool stuff about Tomcat. From my perspective, Tomcat have to maintain and approach the configuration with XML, so we need to have experience when we work with Tomcat as well. There are four files to concern when we work with Tomcat WebServer
Configuration File | Scope | Functionality |
---|---|---|
server.xml | Global | Configures Tomcatβs server-wide settings such as connectors, thread pools, virtual hosts, and global servlet definitions. |
context.xml | Application-Specific or Global | Configures resources and environment settings for individual web applications or globally across all web applications. |
web.xml | Application-Specific | Defines how each web application handles requests and responses, including servlets, filters, welcome files, and error pages. |
tomcat-users.xml | Global | Defines user accounts and roles for authentication and authorization purposes within Apache Tomcat. |
Success
Once upon again, we will reach to detail for what you need to configure with
Tomcat
server, so turn back in next part, practical session to see how we can work with bothMaven
andTomcat
Practical Session
Question
Now, we are kicking off the practical session, in this session, I will focus inside run spring boot java application through artifact application of
Maven
and deploying intoTomcat
Server. That one will totally encapsulate inside docker container, just build and run.
First of all, I am not familiar with Java in my daily job, I have bit experience when I lay in university, that why I can code Java
but canβt familiar or expertise to setup Spring Boot Framework project as well for example, but donβt worry I always try to learn and found some source code, and we can inspect together.
Therefore, today I found once for this lab, shout out to shawon100 who contribute the detailing concept and repository like lab expectation, Explore more at Spring-Boot-Maven-Tomcat-Docker
Prerequisites
First of all, we need to figure out project what is it, learn and inspect deeper inside. So we will separate that into two progress, run with local and run through docker. To handle this situation you need to setup and install couple of tools, including
- Docker
- Java
- Maven
- Linux VM or WSL2
Warning
To easier approach and follow my step, you need to use Linux OS with couple distro about Debian like Ubuntu Or Debian. If you are window user, you can use Linux through WSL2
For install Docker
, that is a simple task and you just need to paste this command and Docker will be on your host
Next, you need to install Java and Maven, in my situation I will try to install maven version 3.9.9 (LTS Version) and it doesnβt specific particular java JDK, so I will try to install JDK18 to see what going on π. BTW, you can double check about Version Range Specification
With Java
, you can install via apt
package with simple command
Wait a bit and you have java version 18 in your host, double check with command
Afterward, we will install Maven
and version specific is 3.9.9
. We can easily install by couple of commands
Now you can have Maven
in your local machine, double check with command
Note
You can add
mvn
into pluginOh-my-zsh
if used to execute completion mode forMaven
in your shell
Now you environment in local machine is ready to practice. Reach next part to see what happen
Play with Project
First of all, you need to ensure clone code from repository
First insight, we can take look about structure of project
As you can see, we have few thing to inspect, such as
Dockerfile
pom.xml
DockerDemoApp.java
andMyController.java
index.jsp
If you take a look about java
and jsp
, the website is kinda simple API with GET Method. When we hit into that URL, it will return the text Welcome
+ parameter name=?
So we reach to pom.xml
and see what is it, you need to take POM (Project Object Model) documentation to understand more configuration
When take a look, we can see couple of things to concern inside this file
- POM version
4.0.0
- Java version
11
- Install package include
spring-boot-starter-web
tomcat-embed-jasper
spring-boot-starter-tomcat
(provided) - a container to provide it at runtime
- Build plugin
spring-boot-maven-plugin
for Spring Boot and Maven - In final, artifact build will name to
sample.war
Now we try to build the artifact file for this project
After wait about 1-2 mins for download package and build war
file inside target/
folder
As you can see sample.war
is archive and now you can run this file to expose WEB API
Following the discussion about StackOverFlow - How do I run a class in a WAR from the command line?, you have two ways to serve your website
Run with standalone command (Not Recommend)
- This command will host
Tomcat
server with one embed inside build progress - But it will expose exception about
java.io.IOException: Unable to open root Jar file
cuz we donβt provide enough lib for this operation
Info
But It seems like not have any problem, so we can consider to run for testing. BTW, we have another way can be better.
Run with bunch of commands to use class inside build (Recommend)
This methodology look simple as well, so you need to follow couple of step to operate your WEB API
- Unwrap the War file
After step unwrap, you can see folder WEB-INF
and META-INF
created, you can use this one to execute your web in next step
- Redirect into
WEB-INF
directory
- Read
MANIFEST.MF
inMETA-INF
folder to getStart-Class
- Run your website with command
Docker with Tomcat Server
Question
When you run standalone website, you need to decompress or use webserver to process
war
file andTomcat
is one handler of this.But itβs not cover the strategy encapsulate and easier to deliver, so we can try to use
Tomcat
to become factor and help WEB API smoothly deliver and deploy.
So with strategy of project, author have write and encapsulate whole project into Docker Image and Itβs make project become easier to deliver and deploy
- Separate into 2 stage
Build
andPublish
- Copy manifest
sample.war
fromBuild
toPublish
Stage - Use
Tomcat
to serve compresswar
file insidewebapps
folder - Use
catalina.sh
to run servlet container. Explore more about bin/catalina.sh
Note
For more information and script to interact with tomcat, you can explore at inside source code of Tomcat at GitHub - Tomcat
Now we can build docker image and try to run that to see what happen
Wait a bit for build docker image success and after that we will run to see how it work
Success
It works and now we can serve Spring Boot API with
Tomcat
Server
Conclusion
Success
Thatβs all for this weekend, hope you find well information and enjoy your time to read my blog. New year for new challenge, new things to learn and truly want to contribute ever and ever, so that is my honor to keep my sprint, hold on the discipline and hopefully yβall guys stand with me for new things in this year 2025.
Quote
Happy to turn back soon with yβall guys, next week will have some special things, I move on for new job and letβs see what thing to motivate new technology fields. Therefore, stay safe, keep learning and we will see each others next weekend. Happy new year and Bye π