What is WEB-INF used for in a Java EE web application?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java EE (Enterprise Edition) web applications, WEB-INF is a special directory that plays a crucial role in the deployment and configuration of web applications on a server. It serves as a secure, private location for storing configuration files, libraries, and class files that are meant to be protected from direct web access. This article delves into what WEB-INF is used for, its structure, and its significance in the world of Java web applications.
The Structure of WEB-INF
The WEB-INF directory resides within the root of the application’s WAR (Web Application Archive) file. When the web server or servlet container deploys a web application, it extracts the WAR file and places WEB-INF in a location that is not directly accessible via a URL. Typically, the WEB-INF directory contains the following subdirectories and files:
- classes/: A directory containing compiled Java classes.
- lib/: A directory for storing JAR files; these JARs will be part of the application’s classpath.
- web.xml: The web application deployment descriptor, which is an XML file that configures servlets, servlet mappings, filters, listeners, and various other components of the application.
Key Functions of WEB-INF
1. Security and Isolation
One of the primary functions of WEB-INF is to ensure security by separating web-accessible resources from those that should remain private. Files and directories placed inside WEB-INF cannot be directly requested using a URL. This ensures that sensitive resources, such as configuration files and server-side scripts, are protected from unauthorized access.
2. Classpath Configuration
Resources in the WEB-INF/classes and WEB-INF/lib directories are added to the application’s classpath by the servlet container. This means compiled classes and JAR files placed here are available to be loaded and used by your application, supporting modular and organized class and library management.
3. Deployment Descriptor
The web.xml file is an essential component of WEB-INF. It provides configuration details that define servlet parameters, mappings, session configuration, and other critical settings. As the deployment descriptor, web.xml is instrumental in controlling how a Java EE web application operates.
Example of a Simple web.xml:
4. Supporting Frameworks and Libraries
The lib subdirectory of WEB-INF is particularly useful for storing additional JAR files required by the application. This is important for supporting various frameworks or libraries that the application depends on. These libraries become part of the web application’s scope, ensuring their classes are loaded and available.
Table: Overview of WEB-INF Directory Components
| Component | Description |
WEB-INF/classes/ | Contains compiled Java class files. |
WEB-INF/lib/ | Stores JAR files needed by the application. |
WEB-INF/web.xml | The deployment descriptor file, which configures the application with servlets, filters, and other settings. |
| Security | Ensures non-web-accessible files are safeguarded from direct URL access. |
| Classpath Utilization | Classes in classes/ and JARs in lib/ are added to
the application’s classpath automatically. |
How WEB-INF Supports Modern Java EE Technologies
With the evolution of Java EE, certain practices around WEB-INF have adapted to incorporate modern features such as annotations, which can reduce the dependency on verbose web.xml configurations. Annotations like @WebServlet, @WebFilter, and @WebListener can often replace corresponding elements in web.xml.
However, WEB-INF maintains its significance by offering a centralized location for configuration that remains applicable, regardless of how the Java EE landscape evolves. It ensures an application is organized, secure, and separated in a way that aligns with industry standards and server expectations.
In conclusion, WEB-INF is a fundamental part of the infrastructure of Java EE web applications, providing essential functionality for configuration, deployment, security, and classpath setup. Its correct and informed use is critical to developing robust and secure Java web applications.

