Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

How-to Set up Web Application Support in IntelliJ IDEA

This How-to describes setting up web application support in IntelliJ IDEA as well as
environment details crucial for this support to properly function.

Introduction
Tomcat configuration details
Creating minimal web.xml for your servlet
Step-by-Step Web Application Support Setup
How to call your web application from browser

Introduction

The following features are available when the web application support is set up:
Web application running/debugging
Absolute references resolution in JSP's
Tag libraries support

Related documents:
IDEA Help: Web Application Options, Web Application Debugging Options, JSP and
Servlets Support.
How-to Series: How-to Manage JSP files in IntelliJ IDEA, How-to Create Servlets in

IntelliJ IDEA.

Tomcat configuration details


Please, keep in mind - this document provides only details critical for running/debugging
your web application using IDEA. Please refer to Tomcat documentation for more details
on its installation/configuration instructions.

1. Consider that you already have a ready web application and its files are located in the
C:\projects\web folder.
2. Download and install Tomcat 4.0.x. At the moment, IDEA supports web application
running/debugging using popular web server Tomcat, version 4.0.x. Versions 4.1.x and
higher are NOT supported.

3. Now you should explain Tomcat where your web application is located and how to
distinguish one web application from another when your server receives a request (you
might have tons of web applications located on your hard drive). To do it, edit
server.xml , a file located in the conf folder of the Tomcat installation:
Wrong settings in server.xml might cause your web application to function
incorrectly with IDEA and/or Tomcat.

4. Editing server.xml in steps:


Anywhere within a Host tag (but not within its child tags) create a Context tag.
If you edit in IDEA, it will look as follows:

There is a couple of sample contexts in standard server.xml shipped with Tomcat.


The path attribute is used to explicitly and uniquely identify a particular web
application for processing by a web server. Let's name the web application
"WebAppl".

To later call your web application from a browser, its URL should contain the path
value right after the web server address.
The docBase attribute indicates where these pages are physically located.

The path can be either an absolute path or a path relative to the default document
root directory (for Tomcat - webapps).
This is how the resulting context tag should look for our sample application.

The tag looks very simple. But it has enough data for Tomcat to identify
unambiguously your web application and to know where to search for its files.

Creating minimal web.xml for your servlet

This part is necessary if you are going to work with servlets.

1. We keep considering the case when your web application files are located in the
C:\projects\web folder.
2. Check that there is a C:\projects\web\WEB-INF folder. If not, create it, because any
particular web application in your project should have its own WEB-INF folder.
3. In the WEB-INF folder of your web application create a file named web.xml.

4. web.xml is the web application deployment descriptor. It defines everything about your
application that a server needs to know.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<servlet>
<servlet-name>YourServlet</servlet-name>
<servlet-class>yourServletPackage.YourServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>YourServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
5. This is the minimal configuration necessary to run your servlet.
The tags servlet and servlet-mapping are associated with each other by the
common tag servlet-name which uniquely identifies a particular servlet.
The servlet-class tag within servlet contains a fully qualified name of a servlet
class. The url-pattern tag within servlet-mapping translates a particular request
URI to the specified servlet class.
In the section How to call your web application from browser you will learn
how to use this mapping.

Step-by-Step Web Application Support Setup


1. Add your web application folder to your project path. Make sure that a WEB-INF folder is
located within this directory.
2. These steps are required only if you are going to work with servlets:
Make sure that the default package containing your servlets is added to the
project's sourcepath.
In the WEB-INF folder create the classes directory and provide it for IDEA as a
compiler output path.
3. Add servlet.jar (for instance, the one located in the common/lib folder of the Tomcat
installation) to your project classpath.
4. Add Web Application support to the project:
Click the Web icon in the Project Properties dialog.
Enable Web Application support by selecting the corresponding check box.

For more details, see section Web Application Options in IDEA Help.

Call the WebApp dialog pressing the Add button. Specify a name for your web
application and a path to the web application folder created in the Step 1.
5. Setting up IDEA run/debug configuration
In the Project Properties dialog, go to Run/Debug menu, WebApp tab.
Press the plus icon to create a new Run/Debug configuration (there is none for
the new web application).
Select a desired web application from the drop-down list (since you can have
more than one web-application in your project).
The default settings for the application server, debugee host, and debugee port
are rather common. But you can change them should they not suit you.
Set the Catalina home for a directory where the Tomcat server is located.
Configuration file settings should be then automatically configured to
Catalina_home/conf.
6. The value of the Application context path field depends on the Tomcat configuration
you are using. There are two configurations possible:
The first configuration allows to run/debug your web application using either IDEA or
Tomcat.

Web application can be run with and without IDEA

Tomcat Configuration

Make sure that you have created the Context


tag in server.xml as described in the
Tomcat configuration details section. If you
work with servlets, you should also have the
web.xml file in the WEB-INF folder of your
web application (see the
Creating minimal web.xml for your servlet
section for details).

Tomcat Configuration Sample

<Context path="/WebAppl"
docBase="C:\projects\web" debug="0"/>.

IDEA Configuration

In the Application context path field select


the context path corresponding to the context
created for your web application (IDEA
automatically detects and lists all available
contexts).

The second is useful when you want to run/debug your web application under IDEA only
and do not need to get into Tomcat configuration details.

Web application is launched from IDEA only

Tomcat Configuration

In server.xml located in the conf folder of


the Tomcat installation, find commented tag
called Tomcat Root Context and uncomment
it. If you work with servlets, you should also
have the web.xml file in the WEB-INF folder
of your web application (see the
Creating minimal web.xml for your servlet
section for details).

Tomcat Configuration Sample

Turn <!--<Context path="" docBase="ROOT"


debug="0"/>--> into <Context path=""
docBase="ROOT" debug="0"/>.

IDEA Configuration

Leave the Application context path field


empty.

7. Write and compile (if necessary) your web-application code. Web application samples
are given in the How-to Manage JSP files in IntelliJ IDEA and
How-to Create Servlets in IntelliJ IDEA.
How to call your web application from browser
1. Launch Tomcat and start your browser.
2. In the address field, you have to define the server address first. For a localhost, type
"http://localhost:8080".
3. Then add the context path of the web application you want to start. In our case, we
keep considering the sample web application above. Its context path is "/WebAppl".
4. The third part of the URL is a bit tricky. It is different for servlets and JSP's.
For JSP's, you need to indicate a page to be displayed (e.g. "/mypage.jsp").
However, if the page is defined as an index page, you can omit its name and type
a slash only. The server will automatically search for "index.jsp".
For our JSP, the final URL will look as "http://your.site.com/WebAppl/
mypage.jsp", or "http://your.site.com/WebAppl/" (if it is an index page).
For servlets, you need to type a value of the url-pattern. The request URI for
calling "YourServlet" (as given in the sample web.xml above) will look like "http:/
/localhost:8080/WebApp/".
You might have several servlets under the same context. To access them
unambiguously, you need to use different URL's. It can be easily done by
modifying the url-pattern value in web.xml for each servlet. For instance,
let's use "/myservlet" instead of "/". In this case, the final URL will look as
"http://your.site.com/WebAppl/myservlet/".

You might also like