Creating your first Tomcat application

Author: Neale Rudd, Metawerx

Date: 27-Sep-2006

This is a short tutorial on creating a new application on Tomcat.

It assumes the site is hosted with Metawerx, but will work elsewhere as long as you can find your webapps folder, have the Tomcat Manager installed, or can manually reset the JVM if necessary.

Create the folder structure for the application

First, locate your webapps folder, this will be referred to as <webapps> below

  • At Metawerx, this is located at /private-cgi-bin/tomcat
  • On a fresh tomcat installation, this is located at <path you installed tomcat to>/webapps

Create a folder under the <webapps>/tomcat folder (eg: /private-cgi-bin/tomcat/MyNewApp)

Tip: if this will be your main application for the website, call the application 'ROOT' (in capitals) instead of MyNewApp, to make it the ROOT application for your site.

Create a WEB-INF folder in the new folder (eg: /private-cgi-bin/tomcat/MyNewApp/WEB-INF)

Your application folder structure should now look like this:

<webapps>
    /MyNewApp
        /WEB-INF

Creating your web.xml file

Create a new file in WEB-INF called web.xml, containing the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">

    <display-name>My First App</display-name>
    <description>This is my first application</description>

</web-app>

Your application structure should now look like this:

<webapps>
    /MyNewApp
        /WEB-INF
            web.xml

This file is the Deployment Descriptor. It describes the application to Tomcat.

As soon as Tomcat notices the <appname>/WEB-INF/web.xml file inside the webapps folder, it will deploy the application automatically, and it will appear in the Tomcat Manager using the name from the <display-name> tag above. This is handled automatically by Tomcat's autoDeploy feature.

We will add a welcome page before testing whether the application is running or not.

Add a welcome page

Create a file called /tomcat/MyNewApp/index.jsp using the following content:

<h1>My First App</h1>
<p>This is an application running on Tomcat</p>

Your application structure should now look like this:

<webapps>
    /MyNewApp
        index.jsp
        /WEB-INF
            web.xml

Test

That's all there is to it! The application should have started automatically. This can be checked by browing to http://yoursite/MyNewApp.

You can also check the status of your new application Tomcat Manager.

  • Access the Tomcat Manager, using http://yoursite/manager/html
  • Look for your new application in the Applications list.
  • If it says true in the running column, your application is running successfully
  • If it says false, there is an error in your web.xml file, or the way you have created the folders. Check that your web.xml file contains the content from above.

If the application is not running, see Troubleshooting Application Deployment on Tomcat.

Success

You can now add some JSP code to your index.jsp file, or make a new JSP file in the application root folder.

For Java Servlets, you will need to add some information in the web.xml file. This is covered in more detail at web.xml. See the <servlet> and <servlet-mapping> tags.

More Information

navigation
metawerx specific
search
tools
help

referring pages
Valid XHTML 1.0 Transitional