Creating a new web application

You already know how to add a new web application to CATALINA_HOME/webapps. In this chapter, you will learn how to make Tomcat serve web applications that are not in CATALINA_HOME/webapps. Create C:/www/myapp directory. Create subdirectories under myapp/ as shown below.

  • WEB-INF
  • WEB-INF/classes
  • WEB-INF/lib

C:/www/myapp is not Tomcat's default web application directory, so Tomcat does not know the existence of myapp application. So there is something you need to do to make Tomcat serve myapp web application. The task is to create one XML file. This XML file is the file that Tomcat needs to manage the web application. This XML file is called a context file because the root element of this XML file is Context. (In Tomcat, Context is a Web application.) The most important information in the context file is the top-level directory (called DocumentBase or Context Root) of the Web application and the Context Path that is URL path information for accessing web application with web browser. These information are set in the docBase and path attributes of the Context element in the context file. If you create a Context file and place it in CATALINA_HOME/conf/Catalina/localhost, Tomcat will automatically recognize and load your web application. If the context file is written incorrectly, Tomcat will not load your web application and will write the cause of the failure to a log file in CATALINA_HOME/logs.

How to create a Tomcat Context file

Let's create a context file for myapp web application.

myapp.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context
    docBase="C:/www/myapp"
    reloadable="true">
</Context>

Copy myapp.xml file to CATALINA_HOME/conf/Catalina/ localhost. If you name your Context file as myapp.xml as above, if you omit the path attribute, the context path is determined as myapp.

For testing, create a test.html file in the C:/www/myapp directory.

test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>myapp TEST</title>
</head>
<body>
<h1>myapp TEST</h1>
</body>
</html>

Restart Tomcat and visit http://localhost:8080/myapp/test.html. If not 404, Tomcat is servicing myapp.

Main Properties of Context
attribute Default Description
docBase   It specifies the root directory (called the Document Base or Context Root) where the web application's source will be located.
path   It specifies the URL path to connect to the web application. This path is called the context path. If the path value is the same as the Context file name, you can omit the path attribute in the Context file.
reloadable false Set to true if you want Tomcat to monitor classes in/WEB-INF/classes/ and /WEB-INF/lib for changes, and automatically reload the web application if a change is detected. This feature is very useful during application development, but it requires significant runtime overhead and is not recommended for use on deployed production applications.

For more information, please visit: https://tomcat.apache.org/tomcat-8.0-doc/config/context.html

Changing the ROOT application

The web application that responds when accessing http://localhost:8080/ is the ROOT application. Stop Tomcat and rename myapp.xml to ROOT.xml. Restart Tomcat and visit http://localhost:8080/test.html. If there is a problem with the ROOT.xml file, then the previous ROOT web application could work.

References