Eclipse, Maven and Tomcat - A Basic Set-Up
This was surprisingly fiddly. One small deviation off the happy path leads to failure - even if the deviation seems wholly reasonable at the time. There may be an easier way, but I wanted to build the most basic web app possible, avoiding Eclipse’s Dynamic Web Project set-up. I wanted a Maven-based project - and not one using a big, complex Maven archetype.
If you do want something more full-featured out of the gate, try Building Jakarta EE 9 Web application with Servlet Containers or something similar.
My approach is basically the same end result as the NetBeans-based Jakarta EE 9 on Tomcat 10 - A Basic Set-Up - and refers to that guide in a couple of places.
- Java 17
- Tomcat 10.0
- Eclipse Enterprise
- Jakarta EE9 (Servlet 5.0)
Download and install “Eclipse IDE for Enterprise Java and Web Developers”
We will use the Java EE perspective.
JAVA_HOME = C:\Program Files\Eclipse Adoptium\jdk-18.104.22.168-hotspot
Window > Preferences > Java > Installed JREs > Add… > Standard VM
JRE home: C:\Program Files\Eclipse Adoptium\jdk-22.214.171.124-hotspot
Save; select checkbox to make it the default; Apply and close.
Window > Preferences > Java > Compiler > compliance level: 17.
Tomcat 10 64-bit Windows zip: https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.23/bin/apache-tomcat-10.0.23-windows-x64.zip
Downloaded and unzipped to here:
Window > Preferences > Server > Runtime Environments > Add…
Apache > Apache Tomcat v10.0
Check “Create a new local server”
(a server is created and added as an entry in the Servers view).
Name: Tomcat v10.0 for Eclipse
Tomcat installation dir: C:\tomcat\eclipse\apache-tomcat-10.0.22
Finish. Apply and close.
The server should be listed in the “Servers” tab at the bottom of the screen. Otherwise, click on the “No servers are available” link.
In the “Publishing” section of the server overview page, make sure “Automatically publish after a build event” is selected.
In the “Server Locations” section leave the selection as “Use workspace metadata”. This is where your webapp artifacts are deployed - the Eclipse-controlled equivalent of Tomcat’s
Do not start Tomcat yet.
File > New > Maven project > Next
The above artifact ID is the one for group ID org.apache.maven.archetypes. It provides a simple file structure.
We just use this as a convenience. We could create the directories manually. In fact we may still need to add a META-INF directory at some point if it becomes necessary.
Select the artifact and then click Next.
index.jsp file in
WEB-INF - we are not using JSPs.
WEB-INF - we will be using annotations not an XML deployment descriptor.
Replace the entire contents of the
pom.xml with the same contents as shown in this article.
To access the “Project context” menu, right-click on project in the Project Explorer.
Project context > Maven > Update project… OK
This updates the project configuration from the
Add a servlet - the same one as shown in this article.
To add the servlet, first create a package in Java Resources >
src/main/java > New > package:
org.northcoder.tomcatbasicdemo > Finish
In that package: New > Class >
HelloWorld > Finish
Add in the
HelloWorld code from the other article. Eclipse may warn you about a missing
serialVersionUID. You can add that:
File > Save (Ctrl+S) or File > Save All (Ctrl+Shift+S)
Project > Build All (Ctrl+B)
In the Servers tab at bottom of page, right-click on the server name and choose Add and Remove…
tomcatbasicdemo(basic_demo) is in the “Configured” list.
Start the server (Ctrl+Atl+R) or the green “go” button on the right hand side of the Servers tab.
Page is served at:
As a test…
Make a change to the servlet. Ctrl+S and then Ctrl+B.
INFO: Reloading Context with name [/basic_demo] is completed
Eclipse deploys the web app artifacts to here: