When you use WAR files to deploy applications on a Tomcat server, the structure of directories in a WAR file must conform to a specific standard. The directory of a web project contains various resources, such as application code, configuration files, and static files.
Structure of a project directory
To compile and package a project with ease, we recommend that you use the following structure to organize files in the project.
tomcat-webapp
└── src
└── main
├── java - A directory that contains source code
│ └── com
│ └── demoapp
│ └── Hello.java
├── resources - A directory that contains resource configuration files
│ └── application.properties
└── webapp
├── 404.jsp - A JSP page that shows an HTTP 404 error message
├── WEB-INF - A secure directory that isolates internal resources from clients
│ ├── classes - A directory that contains compiled classes
│ ├── lib - A directory that contains JAR libraries
│ │ └── mysql-connector-java-8.0.8-dmr.jar
│ ├── views - A directory that contains display templates
│ │ └── index.mustache
│ └── web.xml - A deployment descriptor file
├── index.jsp - A JSP file
└── static - A directory that contains static resource files
├── css - A directory that contains CSS resource files
│ └── demoapp.css
├── fonts - A directory that contains font resource files
├── images - A directory that contains image resource files
│ └── demoapp.png
└── js - A directory that contains JavaScript files
└── bootstrap.min.js
The src/main/java directory contains source Java class files. Each file forms part of an application. These source files are stored in the src/main/webapp/WEB-INF/classes directory and will be compiled into .class files that are accessible by an application. These .class files are stored in the webapp/WEB-INF/classes directory. Then, the webapp root directory that includes all files and subdirectories are packaged and deployed to a web server.
Structure of the webapp root directory
The webapp root directory contains resources, such as HTML files, JSP files, and static resources. These resources and the WEB-INF subdirectory will be packaged and deployed to a web server.
In the webapp directory, all resources are accessible by clients excluding resources that are included in the WEB-INF subdirectory. For example, you can use a client to access 404.jsp and index.jsp. The static subdirectory contains CSS files, image files, JavaScript files, and other resources. These resources are also accessible by clients.
webapp
├── 404.jsp
├── WEB-INF
├── index.jsp
└── static
├── css
│ └── demoapp.css
├── fonts
├── images
│ └── demoapp.png
└── js
└── bootstrap.min.js
Structure of the WEB-INF subdirectory
- The classes subdirectory that contains .class files. The .class files are compiled from the source application code that you develop.
- The lib subdirectory that contains various JAR files associated with the web application, for example, JAR files that contains database drivers.
- Web page template files, such as mustache files.
- A deployment descriptor file named web.xml.
WEB-INF
├── classes - A directory that contains compiled classes
├── lib - A directory that contains JAR libraries
├── views - A directory that contains Web page template files
└── web.xml - A deployment descriptor file