IntelliJ IDEA + Tomcat: creating “hello world” Web Application from scratch



1. Requirements:

1.1. Tomcat 4.0.x or 5.x (Tomcat 4.1.x versions are not supported). This sample uses Tomcat 5.5.4. For 5.x Tomcat versions make sure to download the zipped version instead of installer, since the installer doesn’t contain batch startup script. Also note that you must shut down any other Tomcat instances if they are running as system service, check that http://localhost:8080 doesn’t work.

1.2. IDEA 4.5.x (4.5.4)

1.3. JDK 1.4.x or later (since we use Tomcat 5.5.4, we have to use JDK 1.5)

2. Install IDEA, JDK and Tomcat, for details refer to the bundled documentation

3. Configuring IDEA

Run IDEA, Create a project with the Web Module, follow all the default wizard steps:

We’ll configure a new JDK:

We need to choose a Web Module:

Now it’s time to configure the application server (Tomcat in our case):

Press the Configure button, the following dialog appears:

Press the Add button and from the drop-down list choose Tomcat Server, specify the server installation directory:

Tomcat base directory is used when you need to run several different server instances from one physical Tomcat installation, we don’t need it, and thus it’s the same as the home directory.

Use the default Deployment Descriptor:

Set up the Web Resource directory. It’s a directory containing your JSPs, HTML pages and other web stuff. It will be deployed to the server and accessible as the root of your application by default:

IDEA also silently configures WEB-INF directory as a Web Resource directory and sets it to be deployed as /WEB-INF. If you are using tag libraries, they should be placed under one of the Web Resource directories; otherwise IDEA will not recognize them. Path to tag libraries inside JSP pages is specified from the root of the Web Application, for instance /WEB-INF/taglibs/mylib.tld.

IDEA will copy files from Web Resource directories amongst with libraries, classes and other files you set to deploy in Settings | Paths | Web Module Settings tab to the exploded directory configured above.

This directory can be located anywhere on your drive except the Web Resource directories of your Web Module. In case exploded directory is under the Web Resource directory, recursion will occur while making the project.

If you have servlets or other java code required for the Web Application, configure the source directory and place you java code there.

Java code will be compiled using the JDK we configured for this Web Module and placed into the /WEB-INF/classes directory by default:

Press the Finish button and the project will be created. Now it’s time to create and debug a JSP page. Place the cursor on the resources directory in the Project view and press Alt+Insert. From the drop-down list choose JSP:

Hint: You can configure file templates for JSP and other types in Settings | File Templates.

Enter the page name: index, then press OK button.

You’ll see the index.jsp page in the editor:

Let’s place a breakpoint on the <body> line, click on the left of it, the red circle will appear:

To Run and Debug our page on the server we now need to create a Tomcat Run/Debug configuration:

Press the Edit Configurations item in the drop-down menu in IDEA toolbar and choose Tomcat Server tab, then press [+] button on the left and choose Local:

In the drop-down Application Servers list choose the Tomcat server which we have already configured during module setup:

Note, that if the Start browser checkbox is enabled, IDEA will start browser for you and open the specified Startup page. If you need to start non-default browser, you can configure it in Settings | General.

Let’s take a look at the Deployment tab of the Tomcat Server Run/Debug configuration. On the right you can see that our Web Module will be deployed to the directory which we configured before. Application context specifies the how your application can be reached from the browser. For instance, when the context is set to “/” you have to type http://localhost:8080/ in your browser to open it (you can configure IDEA to open this link for you in the Server tab). If the context is set to something different, like /mycontext, then you have to add this part to the base URL. The URL of your Web Application will look like http://localhost:8080/mycontext / in this case and you will need to adjust the Startup page in the Server tab, otherwise you’ll get the 404 Page Not Found HTTP error. If your startup page is not named as index.jsp or you need to start from different page, add the jsp file name to the Startup URL after the context part.

Normally you don’t need to change anything else, specify the name for your Run/Debug configuration, for instance Tomcat Debug and press OK.

Time to debug, press the debug button on the right of the Run/Debug configuration name:

Press Yes for the following dialog to create output directory for our java classes (we don’t have them in this project):

IDEA will now deploy your Web Application into the exploded directory and will automatically start Tomcat. IDEA automatically configures Tomcat to work with your Web Application deployed into the exploded directory. Then IDEA starts the browser which is trying to open the startup page and the breakpoint is hit:

Press the Resume Program button on the left of the Debugger Panel to continue JSP page execution and you’ll see the JSP output in the browser:

Please note that you don’t need to restart the server if you’ve made some changes to the page and want to test it. Just use Build | Make Project or in case you need to test just the single page changes you can use Package file menu item from the file right-click pop-up menu:

You can also Deploy/Undeploy your Web Modules from the Console tab in the Debug Panel:

When you finished, press the Stop button in the Debugger Panel to shut down the server.

: :