This is to create a web application in Java. The Apache Tomcat will be emddeded to the JAR (not WAR), called uber/fat JAR. Other dependencies, if there is any, will also be included.
We will create a page with the following URL:
This page will display an introduction about embedding Tomcat with Java Web application. This also include button that is link to Books page.
Book page is a list of books with columns: ID, Title, Edition and Action button that when clicked will display a modal consisting more information about selected book.
- JDK 8
- an IDE - Eclipse (Or NetBeans, Intellij IDEA, if you prefer)
Build with Maven
First, we'll create a Java Maven project. Open your terminal and cd to your preferred workspace and run the following commands:
Note: you can choose your own package instead of io/github/julianjupiter/app (io.github.julinjupiter.app)
The project structure should now be:
java-web-app-with-embedded-tomcat └── src └── main └── java └── io └── github └── julianjupiter └── app └── resources └── webapp └── WEB-INF └── pom.xml
Open pom.xml using your favorite editor (mine is Visual Studio Code). Copy and paste the following XML.
Take note that our packaging is jar and we are using Maven Shade Plugin here to package our application into JAR. From its website, it reads: This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
Another thing worth to mention is the mainClass tag where we define our class with main method.
Lastly, we include at least two dependencies for embedding Tomcat: tomcat-embed-core, tomcat-embed-jasper.
Import to Eclipse
To import the project:
Eclipse - File > Import... > Maven > Existing Maven Projects > Browse ... (for Root Directory) > Finish
Let's Start Coding
Create a package server under app. Under this package, we will have an interface Server and class TomcatServer. The interface will have one abstract method, run which will serve as entry point to start our Tomcat server. This opens a possibility to add more servers other than Tomcat, either from scratch or use existing ones (Jetty, Undertow).
We will now create our main class as defined by mainClass in pom.xml.
As this class contains main method, this will serve as entry point to start our application and the server. Here, we instantiate the TomcatServer class and call run method. We pass in args argument from main method to run method. By default, the server will listen on port 8080 but we can override it by passing desired port number to our main method. This is optional, though (stick to 8080).
With the codes above, we can now build and run our application, but since there is no Servlet handler or default JSP page, it will just error page.
To test our application, create a index.jsp file inside webapp folder.
To build and run the application, run the following commands under the root directory of the project:
If the application starts successfully, open your browser: http://localhost:8080/app.
Add Servlet, JSPs and Other Classes
This class is simply a POJO as state container of our data.
These serve our repository. However, the data is just a dummy for this demo.
These serve as intermediary between our controller and repository.
We'll create a base controller that will contain common properties and methods to be used by our controller. We extends this class with HttpServlet so that any class (controller) that extends it becomes an Servlet too.
The BookController extends BaseController class to become a Servlet. This will serve as the controller. Since our application will only display list of books and individual book, only doGet method will be implemented. This method intercepts all HTTP GET request.
Our templates will be written in JSP.
Let's create first templates folder under /webapp/WEB-INF. Under this new folder, we'll create four (4) folders - book, components, error, and includes.
The new structure of our Maven project:
java-web-app-with-embedded-tomcat └── src └── main └── java └── io └── github └── julianjupiter └── app └── controller └── domain └── repository └── server └── service └── util └── resources └── webapp └── WEB-INF └── templates └── book └── components └── error └── includes └── pom.xml
These are the common parts of a page.