Tutorial: IntelliJ IDEA Community Edition with Tomcat deployment

На русском

This tutorial will help beginner developers of Java EE applications.

IntelliJ IDEA is a great IDE for a Java developer, however its Community Edition is somewhat limited in regard to enterprise and web development toolbox. You can easily develop desktop applications in Community, but confronted with the need to develop an enterprise web application with Java EE you suddenly find out that Community Edition can't provide you with an application server or automatic deployment or anything, really.
In this tutorial I'm going to show you how to sidestep this issue by integrating IntelliJ IDEA Community Edition with Tomcat (via Maven) to develop a sample website with servlets.
As most projects within IDEs do, this one starts with New -> Project item from menu bar.
Pick Maven on the left side and create a project from maven-archetype-webapp archetype (pic. 1):

Pic. 1. Create a Maven project.

The following screenshots demonstrate setting group id, artifact id, project name and location. Group id is the unique identifier of your project, usually the name of the package as per Java package naming conventions. Artifact id is the name of the JAR file that gets deployed into Maven repository, in this case mywebapp. See how to pick group id and artifact id here.


After creating a project, you end up with the basic directory structure required for web application: WEB-INF folder, web.xml file and even an index page(pic. 2).

Pic. 2. Basic web app structure.
 
 Maven can build that, but there's still no directory for Java sources! That's what you have to do next (pic. 3). Create a folder java under My web app/src/main/ and mark it as source root. That's where all servlets go.

Pic. 3. Create a source root.

Let's proceed to make a sample servlet HelloServlet.java (pic. 4) and paste the following code there:



Pic. 4. Create a sample servlet.

The code is correct, but it's highlighted in red! Why? We forgot to add Java EE library as a dependence. Open your pom.xml and make it look like that:



Modify pom.xml.

Even though you don't need it to compile the project, your pom.xml now contains lines that pull tomcat7-maven plugin. Now let's make use of it: create a new Maven Run/Debug Configuration, name it whatever you want, put tomcat7:run in Command line field, and, finally, add a Maven goal clean to the list of things to do before launch (pic. 5):

Pic. 5. Create a run configuration.

Congratulations! You now have a working Java EE webapp project with run config that builds the entire thing and deploys it to Tomcat:


Now you should be able to simply press Shift+F10 (or Run button, I'm not going to judge) and see index.jsp in your web browser of choice after clicking the link http://localhost:8888 in build output window:


What about servlets? To see the servlet in your web browser you have to let Tomcat know where to look for it first. For that, change src/main/webapp/WEB_INF/web.xml and src/main/webapp/index.jsp to look like that:





Modify web.xml and index.jsp.

Run the project again and, voila, you should now be able to access your servlet and see that it works:


Now your humble IntelliJ IDEA Community Edition can do as much as full edition can - automatically build and deploy web apps to Tomcat servlet container.
Download complete IntelliJ IDEA project

Starting a web browser after deployment

This follow-up on the tutorial above is meant for people who are not quite satisfied with the degree of automation in the process of deploying web application. Yeah, you press Run and Tomcat starts, but you have to press the link manually? Let's fix it and make Run configuration open a tab with web application in system's default web browser.
Assuming you have done the steps above (or downloaded the template ) you have to do the following:
  1. Replace text between <plugins> </plugins> tags in pom.xml:



  2. Go to Run/Debug Configurations and change Command line parameter to verify :


  3. Create test.bat file in the root of your project and paste the following there:

N.B. To avoid having to clear my browser's cache after every redeploy, I changed system's default browser to Chrome in incognito mode:
Not clearing browser's cache in browser's normal mode will make it store cached versions of your scripts, and that can lead to hours of head scratching.
That's all! Run your project and a web browser with your app starts up. That's what you'll see in IDEA:



To shut Tomcat down you can either press Enter in "Run" window (normal stopping) or stop button in top right corner of IDEA (abort, use in extreme cases).


Comments