war file structure

Each web application, when uploaded to the web server, is packaged into a single .war file. WAR now stands for Web Application Resources, although it used to be Web ARchive. In fact, this is a zip archive that contains a packaged web application.

Here is what the typical content of a war file looks like:

/index.html
/guestbook.jsp
/images/logo.png
/js/jquery.js
/WEB-INF/web.xml
/WEB-INF/classes/com/codegym/Util.class
/WEB-INF/classes/com/codegym/MainServlet.class
/WEB-INF/classes/application.properties
/WEB-INF/lib/util.jar
/META-INF/MANIFEST.MF

Inside the war file are static web resources such as .html, .css, .js files, and so on. Also there can be pictures, videos and in general any files. They can be in the root or in subfolders, it doesn't matter. Tomcat will simply serve them up if they are requested.

Let's say your web application is loaded on the web server under the name apple, then when http://localhost/apple/images/logo.png is requested , Tomcat will return the file /images/logo.png .

Separately, it is worth noting the WEB-INF folder . It is designed to store Java code in it. Tomcat will not give out its contents.

/WEB-INF/classes/ directory for compiled non-JAR Java classes, including servlet classes and resource files needed by the loader before running the application
/WEB-INF/lib/ place to store jar libraries
/WEB-INF/web.xml deployment descriptor

war file structure and maven project

Now let's move on to the directory hierarchy of the Maven project. You can see the full catalog layout in the official manual. Here we will get acquainted with it in a somewhat abbreviated version, excluding test resources. So, the standard Maven directory hierarchy looks like this:

src/main/java source codes of application classes and libraries in accordance with the generally accepted package hierarchy
src/main/resources application resource files: database settings, localization files, etc.
src/main/webapp web application resources (JSP files, text files, scripts, etc.)

As you can see, it differs significantly from the structure of the WAR file that you know. But what actually happens when compiling a web application is simply moving and concatenating files into the structure defined in the Java EE specification.

The src/main/webapp directory defines the context root of the web application (when deployed to a server, the context root is the same as the name of the WAR file) and already contains the WEB-INF directory within it. That is, the contents of src/main/webapp are completely transferred to the web application.

All your Java classes are compiled into class files and, keeping their package structure, are moved to the /WEB-INF/classes/ directory . The JARs of the included libraries, which are defined in the Maven pom.xml dependencies as we defined above, are moved to the /WEB-INF/lib/ directory .

Application resources src/main/resources are moved to the application's classpath, specifically to the same /WEB-INF/classes/ directory .

To make it completely clear, look at this diagram, which will help you understand how and what goes where when building a project: