Using Tomcat Virtual Hosts with IntelliJ IDEA

If you’re like me, when you’re building a IntelliJ IDEA Java Web Application with Tomcat you don’t want to use localhost:8080. I want a unique url for each project, and since web sites are so easy to add in Linux Apache there’s no excuse not to.¬†Tomcat defaults to post 8080 and when you fire up a Java Web Application in IntelliJ the address is http://localhost:8080. Instead of that we want to use http://javaweb for our project Javaweb.

First thing to do is to configure Apache and Tomcat to share port 80 which makes Tomcat Virtual Hosts possible. You can read how to do that our last episode, Configuring Tomcat Virtual Hosts With Apache. With that done we can wire-up our IntelliJ Java Web Application to use our http://javaweb virtual host.

Here’s our simple project structure…

our simple .JSP page…

and what we want to see when we run the app in IntelliJ as well as where we want to see it, http://javaweb.

Configuring the Tomcat Application Server in IntelliJ

If you’ve not already done so, to build Java Web Applications with Tomcat you need to configure the Tomcat Application Server in IntelliJ Settings. Real easy, since we’re doing little more than telling IntelliJ where Tomcat is located.

In the application’s Run Configuration we enter the startup url.

Now we’re ready to run index.jsp and view our “Yousa from index.jsp!” page.

Configuring http://Javaweb in Tomcat and Apache

We covered these points in Configuring Tomcat Virtual Hosts With Apache, but let’s look at the configuration specific to our IntelliJ Java Application. If you see the following, don’t panic. Let’s review the basics.

Here’s our /etc/apache2/sites-available/javaweb site definition file in Apache. When configuring ProxyPass be sure to remember that our Java Project base directory is {PROJECT_ROOT}/web.

And if using /web in ProxyPass, our Tomcat Symbolic Link should point to our project root.

There ya go! Enjoy using your project’s Virtual Host!