This tutorial walks through the process of using Tomcat to run a server on your computer. Running a local Tomcat server is handy for testing things out without needing to update (or pay for) a live server.
Tomcat is one of the most popular Java servers out there, and it provides a standard environment that most Java server code will work with. If your code works with Tomcat, chances are it’ll work with most environments.
Tomcat is bundled as an
apache-tomcat directory inside a
.zip file, which you can download from here.
Download that file, and then unzip the directory anywhere. I put mine on my desktop. You can always move it later.
Now that you’ve downloaded Tomcat, you can run a Tomcat server!
startup.bat file in the
bin directory and run it, either through the command line or by double-clicking it.
Then navigate to localhost:8080 and you should see something like this:
That means you’ve successfully run a Tomcat server!
You can think of a web app as a website and everything in it, including its server code. Now that you have a Tomcat server running, you can add web apps to it!
In your Tomcat folder, notice the
webapps directory. This is where your web apps will go.
To add a web app to your server, create a folder inside the
webapps directory, and then add your files inside your folder.
For example, create a
HelloWorld folder inside your
webapps directory. Inside the
HelloWorld directory, save this HTML to a file named
<!DOCTYPE html> <html> <head> <title>Tomcat Hello World</title> </head> <body> <h1>Happy Coding</h1> <p>Hello world!</p> </body> </html>
Your directory structure should look like this:
(files you don't care about)
(more files you don't care about)
(some example web apps)
Now open your web browser to http://localhost:8080/HelloWorld/index.html, and you should see this:
Notice that you didn’t have to restart the Tomcat server. It should automatically detect changes to the
Congratulations, you just wrote your first web app!
So far, the whole web app is a single HTML file, but if it helps, you can view and download this project here:
Notice that the URL for your web app contains the
HelloWorld directory. This is useful if you have multiple web apps running on the same server, but you also might want to create a “top level” web app that doesn’t include its name in its URL. To do that, create a web app named
Tomcat comes with a default
root webapp, which contains links to documentation. You can delete that and replace it with your own
root folder, save this HTML to another file named
<!DOCTYPE html> <html> <head> <title>Root Web App</title> </head> <body> <h1>Root Web App</h1> <p><a href="http://localhost:8080/HelloWorld/index.html">Here</a> is a link to the Hello World app.</p> </body> </html>
Now open up your web browser to http://localhost:8080/index.html, and you should see this:
Note that this URL does not contain the
Also note that you can link between web apps, just like you can link between web pages- because that’s what these are!