Tutorial: IntelliJ IDEA Community Edition with Tomcat deployment

На русском

Want to build your first web application? Why not try Spring Boot? Refer to my newer, absolute beginner-oriented tutorial.


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

  1. Hello, I tried to follow this and added the below code to the pom.xml but got compile error: java: package javax.servlet.http does not exist. (I'm using Tomcat 6.0.53 and Maven 3.5.0. I downloaded Java EE and unzipped and got a Glassfish folder)

    Do you know what is wrong?


    javax
    javaee-api
    6.0.53
    provided




    org.apache.tomcat.maven
    tomcat6-maven-plugin
    6.0.53

    9999





    org.apache.maven.plugins/groupId>
    maven-compiler-plugin

    3.5.0
    3.5.0



    ReplyDelete
    Replies
    1. Hi!
      Actually, you don't need to install anything for this use case. Tomcat Embedded and Java EE libraries are pulled during project assembly as maven dependencies.
      In case you want to use external Tomcat, JavaEE (and Glassfish, for some reason?) from your installation, you'll need to find another tutorial, I'm afraid.

      Delete
    2. Hey there, sorry my post looks so spaced out, I pasted the code from the pom.xml but the angle bracket tags didn't show up.

      I found the problem, I specified the wrong version for javaee-api. Now everything works but only if I remove the slash from the path section which comes after the port (pom.xml), otherwise it keeps running the generic localhost tomcat page. Anyway, thanks for this, it was a lot of help in learning how to make my first little app.

      Delete
    3. Filtering angle bracket tags from user input seems pretty obvious for any web application - you may use any online paste service next time.
      Glad I could be of help - that's what my post is for, anyway. :)

      Delete
    4. Hey, it worked for me, thanks for guide.

      However, using this method I dont have access to Tomcat managment site that should be under localhost:8080. Do you know how to enable it ? Do I need to make Run configuration in Intellij using Tomcat Server instead of Maven Run/Debug Configuration ?

      Greetings :)

      Delete
    5. Hello there!

      Tomcat management's url defaults to http://localhost:8080/manager according to http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/usage.html .

      AFAIK, Tomcat run configuration is only available in EAP or Pro editions of IntelliJ, so if you have it, you don't even need this guide!

      Delete
  2. This was really helpful. Makes you appreciate all of the bells and whistles that come with a full edition IDE like IntelliJ Ultimate or Eclipse, but also teaches you how to get more familiar with the nitty gritty of projects, dependency management, maven, and coding in general. Very good tutorial; worked for me. Thanks!

    ReplyDelete
  3. thank you very much it was helpful

    ReplyDelete
  4. In pom.xml

    javax
    javaee-api
    6.0
    provided


    How in the hell can javaee-api work in IntelliJ Idea Community Edition dude ?

    ReplyDelete
    Replies
    1. Believe it or not, my dude, but you actually need it for servlet-related classes (to be exact, it contains HttpServlet, HttpServletRequest and HttpServletResponse).
      The dependency's just a set of classes, and you can make use of them everywhere. It's not like they contain some kind of bizarre condition of working if you only have a specific IDE.

      Delete
  5. Thank you! I was able to do this tutorial using IntelliJ 2017.2.6 and tomcat 9. I didn't integrate tomcat into IntelliJ; I used the tutorial just to generate the war and then deploy independently to an external tomcat.

    This is a very useful tutorial, thank you.

    ReplyDelete
  6. Hi Vadim,
    Thank you for guide explained. However does it missing something more for the community version of IntelliJ, because I try to pass an attribute from the servlet to the jsp page via getRequestDisplatcher forward but the expession such as ${"AttributeNamehere"} in the JSP remains not updated.
    Do you have any idea ?

    ReplyDelete
    Replies
    1. This guide was born out of my experience with building a marketplace application with JSP and servlets.
      So, everything should work as I myself used the same set up (in fact, the app is still up on heroku https://guarded-coast-90074.herokuapp.com/ although I think I rewrote that to use Spring MVC since then).
      And yes, I'm aware that servlets and JSP can be tricky sometimes, try changing your code and see if it works then - this approach always worked for me :-)

      Delete
  7. Hello there !
    Your tutorial worked like a charm until the second part "Starting a web browser after deployment".

    On Linux, the script seems not to run at all (no "exec-maven..." output).
    Moreover, with the "verify" parameter, tomcat never starts (no response from localhost:8888).

    I read carefully all the step and I know bash scripting, but I don't managed to get this part working on my environment.

    Do you have any idea how to solve this problem ?

    ReplyDelete
    Replies
    1. I'd try another maven plugin for browser startup, I guess - this one was only tested on Windows.

      Delete
  8. Hi, is it possible to use this tutorial on graddle project instead of maven?

    ReplyDelete
    Replies
    1. Not sure about that - I'd only used maven in my dev work, but I'd like to see if something comes out of it. If you manage to get it working with gradle, please drop me a line about it and I'll link your writeup/add a section on gradle into mine.

      Delete
  9. A similar approach is possible with Gradle using a combination of "Gretty" and "War" gradle plugins. We're using this method for our project without any issues

    Gretty Plugin Link - https://plugins.gradle.org/plugin/org.akhikhl.gretty

    Gretty Official Doc - http://akhikhl.github.io/gretty-doc

    Gradle Official Doc - https://guides.gradle.org/building-java-web-applications

    ReplyDelete
  10. In pic. 4 when I am to create the sample servlet when I right click the java folder there is not option for servlet in the new drop down menu. Am I missing something?

    ReplyDelete
    Replies
    1. A servlet is just Java code, so the option you're looking for in that drop-down is Java class.

      Delete
  11. Nice post! Congrats!
    How do I debug running tomcat classes in this model?

    ReplyDelete
    Replies
    1. Thank you! I don't remember if you can do that in that configuration, frankly, it's been years since I wrote this post.
      You can try hitting "Debug"... But I'm pretty sure if you tried Spring boot instead (that post I'm recommending at the top) it would work.

      Delete
  12. Hi there when running my app , the console states no tests were found

    ReplyDelete
  13. clear and informative. Thank you.

    ReplyDelete
  14. this is the best tutorial. thanku it helped a lot

    ReplyDelete
  15. Nice little tutorial, and not much different even in IntelliJ CE 2019. One gotcha to note: if you're going to use annotations instead of specifying your servlet in XML, then you cannot use the web.xml from this tutorial. You need to use a web.xml which specifies a v3 servlet instead.

    ReplyDelete

Post a Comment