How to Tomcat in IntelliJ IDEA Ultimate
미연
·2022. 4. 26. 12:27
In this post, we’ll look at how to work with Apache Tomcat in IntelliJ IDEA Ultimate.Let’s start by looking at how to set up Tomcat when creating a new project.We’ll be using IntelliJ IDEA Ultimate since it provides integration with Tomcat by default.Let's create a simple web application that runs on Tomcat. I'll click New Project, choose “Java Enterprise” as my project’s framework, then I’ll give my project a name.I can choose any of these project templates. For this tutorial, I want to create a RESTful web application so I’ll keep the REST service template selected.The application server field is where you can configure Tomcat for a new project. To configure a new application server, select New.. then choose the application server you’d like to use. I’ll select Tomcat Server from my list.IntelliJ IDEA prompts me to enter my Tomcat Home. If you don’t have Tomcat already downloaded, I’ve included a link to the Tomcat download page in the description.In my case, I’ve already downloaded and extracted Tomcat on my machine. So, I’ll grab the location of my Tomcat directory and paste it into IntelliJ IDEA as my Tomcat Home.If you see any errors on this step, make sure you’ve entered the correct Tomcat location.I can tell that IntelliJ IDEA found my Tomcat home successfully since it's showing the Tomcat version I'm expecting. So I'll go ahead and click OK.Now that I have Tomcat configured, I can finish creating the project.I’ll keep the defaults for the rest of the fields. I’ll change my group name and I’ll be using Java 17 for this app so I’ll keep that selected and click Next.In the next window, I’ll select Jakarta EE 9 from the drop down menu so I can use the latest Jakarta EE framework. Since we selected the REST service as our template in the previous step, IntelliJ IDEA pre-selects the dependencies needed for a REST service, that includes Jersey which is a JAX-RS implementation and Weld which is a CDI implementation.These are all the dependencies I need for this application so I'll click Finish to create this project.IntelliJ IDEA creates a simple web application which outputs “Hello, World!” when it receives a GET request.In our Services window, we see the Tomcat server we configured along with our web application.Let’s go ahead and run this application to see what happens. I’ll use the Shift+F10 shortcut for Windows and Linux or ^R for macOS which will start my Tomcat server.We can now see our Tomcat server logs in the services window which shows that Tomcat is starting and our application getting deployed.Once our application is deployed, IntelliJ IDEA automatically opens up a browser with the URL at the application’s root context.In this case, I see a 404 page which is actually a good sign since it tells me that Tomcat is running and listening for requests. However, I’m getting a 404 because my application has no view to display, for example, I don't currently have an index.jsp file. This is fine because I just want me app to be a simple service with some REST APIs.So let’s figure out how to reach my application’s endpoint that displays the Hello World message.If I go back to my application code,I can find the ApplicationPath in my HelloApplication class - which is “/api”.Then if I go to my HelloResource class, I can see that the Path is set to “/hello-world”.So I can make a GET request to the HelloResource by adding “api/hello-world” to the URL in my browser.We can now see the “Hello, World!” message coming from our web application running on Tomcat.A lot of times you may already have an existing application that you’re trying to setup to work with Tomcat. Let’s take a quick look at how to do that.Here, I have a different web application that I cloned from GitHub. I want to deploy this application to Tomcat. I can do that by going to Run and choosing Edit Configurations… To create a new Tomcat Configuration, I can click on the Add button then scroll down to Tomcat Server. Here you’ll see two configuration types one for local servers and one for remote servers.I’m running my Tomcat server on this machine so I’ll select Local.I’ll give my configuration a name. Then,I’ll configure my Tomcat Home similar to how we did it earlier in the New Project wizard.Then, I’ll go to the Deployment tab to add the application artifact I want deployed to the server. I’ll click on the add button, and choose Artifact… I’ll select MyWebApp:war exploded since it allows me to update the application code without redeploying or restarting the server.Once I have my artifact added, I can review the rest of my server configuration then click OK.Now let’s run this application. I'll press Shift+F10 which starts our server. Once our application is deployed, IntelliJ IDEA opens our browser at our application’s root context which in this case displays an actual page because this app has an index.jsp file.Looks like our application is deployed successfully!Now that we know how to set up our IDE to work with Tomcat for both new and existing projects, let’s look at how to work with Tomcat during application development.Let’s go back to the application we created earlier.Let’s say I want to change my application and add a new POST method that takes in a name as a parameter and returns a custom greeting.I want to pause here and make an important note about updating your applications.If you’re adding a new method or making hierarchy changes to your application, you’lll probably need to restart your Tomcat server in order to see and test out those changes.On the other hand, if you’re only making minor changes for example changing a method body, you don’t have to restart your server if you have your environment configured properly, which I'll show you in a few steps.Since I added a new POST method to my application, I need to restart Tomcat in order to see my change and try out my new method.But before we do so, let's take a look at our Tomcat configuration and change what happens when the server restarts.You can access your Tomcat server configurations by going to your Services window and right clicking on your Tomcat server node. Then you can choose Edit Configurations… This window allows you to configure various options for Tomcat. For example, I want to disable the option that automatically opens up my browser after the server starts.Instead of using the browser to interact with my application, I’ll use the HTTP Client which is a lot more useful when developing RESTful applications.Another useful configuration here is the ability to specify VM options you’d like to pass in to the server when it starts.These two options are very helpful when you’re actively developing your application.This On ‘Update’ option says that if I trigger an update in the IDE, IntelliJ IDEA will recompile my java classes and update my classes and resources so I can see my application changes without needing to restart my server.An update is triggered when you press the Ctrl+F10 or Command+F10 shortcut or when you click the Update button in the services window.If you don’t want to manually trigger an update and instead want the application classes and resources to be updated whenever you click away from your IDE or the IDE loses focus, you can configure the on frame deactivation option to Update classes and resources.I usually change this configuration when I’m working on a view page and flipping back and forth between the IDE or the browser. I’d recommend trying out both options and seeing what works for you when developing your applications and testing them out.If you ever want to change the HTTP port that Tomcat runs on, this is where you can do it.I’m happy with my current Tomcat configuration so I’ll click OK.Now we’re ready to restart our Tomcat server to test our new POST method.This time, I’m going to start Tomcat in debug mode which I can easily do using this button in my Services window. I’m starting Tomcat in debug mode so I can take advantage of hot swap later when I make changes to my method implementation.Now that Tomcat has started, let’s see if our new POST method works. I want to make a POST request to my app which I can do right in my IDE using the HTTP Client. This is one of my favorite features since it lets me create HTTP requests right within the IDE. I’ll create a new file, call it requests.http and in the file, I can make a POST request and pass in my name as a parameter.I'll run my HTTP request using this button.I can see my POST request ran with a response that includes my custom greeting which means that my application’s POST method handled the request successfully.Now, let’s say this time, I want to change my method implementation a bit. For example, I want to change the return message for my POST method. In this case, I’m only changing the method body and not adding any new methods. So in order to see my change, I just need to trigger an update.I can do that by pressing Ctrl+F10 or the update button on my services window.This will bring up a dialog asking me to confirm what I want to happen on this update.I want all my classes and resources to get updated so I’ll click OK.I will run my POST request again, this time from the Services window, and we see my application changes reflected in the updated message.So to summarize, when you’re actively developing an application, I would recommend starting Tomcat in debug mode and configuring your IDE to update your classes and resources so you can see your application changes right away. But remember that there are certain changes that will still require a server restart such as adding new methods to your application.If you ever want to restart or stop your Tomcat server, you can do that in several ways. You can right click on the Tomcat server in the Services window, or you can use the shortcuts or you can use the buttons in the Services window. If you stop your Tomcat server, you'll notice that the icon on the Tomcat server node switches from a green icon to a red icon.Finally, let’s take a look at how to debug an application running on Tomcat.Let’s say I want to debug this line in my application. I’ll go ahead and add a breakpoint.Then, I'll start my Tomcat server in debug mode.Now that Tomcat has started in debug mode,I need to trigger my breakpoint by making a request to my POST method.I can run the same request from earlier or I can create a new request.Let’s create a new one with a different name as my parameter.I'll run this request which triggers my breakpoint.And now I can debug my application using the Debugger tool window.We see that the name parameter is set to Evan and we can step through the application as we like.Once we're ready, we'll resume our run which finishes running the second POST request and outputs the results in a new request tab.We now see the expected results from my second POST request.In this post, we explored how to work with Tomcat in IntelliJ IDEA Ultimate so you can easily interact with your Tomcat server without leaving your IDE.
'OS' 카테고리의 다른 글
How to Install Bootstrap and JQuery (0) | 2022.04.28 |
---|---|
Free SSL Certificate Installation in CentOS 7 (0) | 2022.04.27 |
How to Install WebStorm IDE on Windows 10 (0) | 2022.04.25 |
How to HTML CSS stands for Cascading Style Sheets (0) | 2022.04.22 |
Coding up a Basic web Template Beginners with HTML, CSS, Flexbox and Bootstrap (0) | 2022.04.21 |