Tomcat Setup


Tomcat Setup


Created on June 20, 2021
Updated on July 27, 2021

tutorial java server tomcat


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.

Download Tomcat

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.

Run Tomcat

Now that you’ve downloaded Tomcat, you can run a Tomcat server!

Find the startup.bat file in the bin directory and run it, either through the command line or by double-clicking it.

Running Tomcat

Then navigate to localhost:8080 and you should see something like this:

Tomcat homepage

That means you’ve successfully run a Tomcat server!

Hello World Web App

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 index.html:

<!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:

  • apache-tomcat-x.y.z/
    • bin/
      • startup.bat
      • (files you don't care about)
    • (more files you don't care about)
    • webapps/
      • (some example web apps)
      • HelloWorld/
        • index.html

Now open your web browser to http://localhost:8080/HelloWorld/index.html, and you should see this:

Tomcat hello world

Notice that you didn’t have to restart the Tomcat server. It should automatically detect changes to the webapps folder.

Congratulations, you just wrote your first web app! :tada:

Example Project

So far, the whole web app is a single HTML file, but if it helps, you can view and download this project here:

Root Web App

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 root.

Tomcat comes with a default root webapp, which contains links to documentation. You can delete that and replace it with your own root directory.

In that root folder, save this HTML to another file named index.html:

<!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:

Tomcat root web app

Note that this URL does not contain the root directory.

Also note that you can link between web apps, just like you can link between web pages- because that’s what these are!

Examples

Comments

Happy Coding is a community of folks just like you learning about coding.
Do you have a comment or question? Post it here!

Comments are powered by the Happy Coding forum. This page has a corresponding forum post, and replies to that post show up as comments here. Click the button above to go to the forum to post a comment!