How to run/debug your web application with IntelliJ IDEA and Tomcat

Madhawa Perera
6 min readMay 8, 2019

--

This is a very short article on how to run and debug your web application using IntelliJ IDEA.

In order to learn how to run/debug a web application, you need to have a web application. Obvious! :D If you are interested in learning how to create a simple Java RESTful Web Service refer here. For this article, I have created a very simple JAX-RS/Jersey web application. You can find the source code here on GitHub. Fork the repository and clone it and modify it as you want. Otherwise, simply download the .zip file and extract and open it on IntelliJ IDEA.

Prerequisites

  • IntelliJ IDEA: A Java IDE by JetBrains
  • Apache Tomcat Web Server version 7: Open source servlet container
  • Download the sample web application source code from here.

Setting up Tomcat and adding artefacts

If you are using macOS then you can use Homebrew to install the tomcat server. Also, you can visit http://tomcat.apache.org/ and download the zip file and extract it to a location based on your preference.

Now open IntelliJ IDEA. Open the downloaded project jaxrs-sample-webapp.

Then go to the 'Run' menu and 'Edit Configurations'. Refer to Figure 1 below.

Figure 1

In the popup window, click on the ‘+’ button on the top left corner. Refer to Figure 2 below.

Figure 2

In the drop-down select Tomcat Server >> Local as shown in the Figure 3 below.

Figure 3

You will be directed to the following window as in Figure 4. Please note that you can always use the 'Configure' button and provide the path to the bin folder of your apache tomcat installation and change the servers as needed. If you have multiple servers, name them properly to avoid confusions.

Figure 4

In the above window, you will see a warning message saying "No artefacts marked for deployment". Scroll down further or click and expand on "Before launch: Build. Activate tool window". Refer to Figure 5 below.

Figure 5

Click on the "+" button on the bottom of the window, below “Build” and select "Build Artifacts". Refer to Figure 6 below.

Figure 6

On the popup window select the 'simple_webapp:war' checkbox as shown in Figure 7 below. Click OK.

Figure 7

Then go to the 'Deployment' tab as shown in Figure 8 below and then click on the "+" button, which is on top of the Application Context. Then select "Artifact…" from the drop-down.

Figure 8

On the popup window select the ‘simple_webapp:war’ as shown in Figure 79 below and click OK.

Figure 9

N.B. Make sure to change the "Application Context" to /simple_webapp. This will be by default /simple_webapp_war. Refer to Figure 10 below.

Figure 10

Click OK, then Apply and then OK again. Now you should see similar IDE interface as below in Figure 11 and 12.

Figure 11

If you are all set up to this point and everything happens smoothly, you are ready to run your web application. Before running the web application lets put a HelloService.java line 18 a debug point. Just click on where you can see a red ball in Figure 12 below.

Figure 12

All set to run. Get set, ready and Run… Refer to Figure 13 below. Click on the "Run" menu and then click "Run…".

Figure 13

Since we click on "Run", you do not need to worry about the debug point. The Tomcat server will be started in normal mode. Now the Tomcat server will be started and your web application will be deployed. Now it is time to test your REST API. You can use any REST client like Postman, CURL etc. Here, I'm going to use a web browser because it is the easiest. Open the browser and copy and paste the following endpoint.

http://localhost:8080/simple_webapp/rest/hello/madhawa

You should get the output as shown in Figure 14 (Make sure you run Tomcat on the port 8080).

Figure 14

To debug, you need to start the Tomcat server in debug mode. Since we are handling everything inside IntelliJ IDEA, you can go to the "Run" menu and then click on "Debug…". Refer the Figure 15 below. Now the Tomcat server will be started in debug mode and your web application will be deployed.

Figure 15

Like you did before let's call the endpoint (API). http://localhost:8080/simple_webapp/rest/hello/madhawa

This time you will be directed back to the IDE. Now your program is paused in the debug point that you have added before. That is on line 18 in HelloService.java file. Refer the Figure 16 below.

Figure 16

You can debug your code line by line if you want by using the different arrow icons you can see in Figure 17.

Figure 17

At any time you can resume program or execute line by line or whatever you like to do. Here you can see (Figure 17) the values that your variables hold, object references etc. If you want, you can add different values to these variables manually and test. That will be a separate topic like what you can do while debugging. So I'm not going to describe what you can do here. Once you executed all the lines or if you resume the programme you should get the final outcome on the browser as in Figure 14.

Well, that's a very short and quick explanation of how you can run/debug your web application with IntelliJ IDEA and Tomcat.

If you like this article and the content that I create, you can buy me a virtual coffee ️😊;

Cheers!

--

--

Madhawa Perera
Madhawa Perera

Written by Madhawa Perera

Research Engineer, HCI Researcher, and Sessional Academic at ANU. Passionate about crafting human-centered AI experiences through Mixed Reality (AR/VR)

Responses (3)