Creating a new web application

You already know how to add a new web application to CATALINA_HOME/webapps. This chapter describles 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, Tomcat does not know the existence of myapp application. There is something you need to do to make Tomcat serve myapp web application. The task is to create one XML file that Tomcat needs to manage the web application. The root element of this XML file is Context. In Tomcat, Context represents a Web application. So, we call it a context file.

The essential information in a context file is the top-level directory (called DocumentBase or Context Root) of the Web application and the Context Path, URL for accessing the web application with a web browser. The docBase attribute of the Context element has DocumentBase information. The path attribute of the Context element has Context Path information.

You create a Context file and place it in CATALINA_HOME/conf/Catalina/localhost. Then Tomcat will recognize and load your web application. If the context file is incorrect, Tomcat will not load your web application and 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 the 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 and omit the path attribute, Tomcat determines the context path 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.

Three primary attributes of the Context element

Attribute Default Description
docBase   It specifies the root directory (called the Document Base or Context Root).
path   It determines the URL path to connect to the web application. This path is called the context path. Tomcat specifies the context file's name as the path attribute's value if you omit the Context element's path attribute.
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 helpful during application development, but it requires significant runtime overhead. Therefore set to true is not recommended for deployed production applications.

For more information, please visit: https://tomcat.apache.org/tomcat-8.5-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