Java

What is WEB-INF used for in a Java EE web application

27 September 2026 · 11 min read

What is WEB-INF used for in a Java EE web application

In the intricate world of Java Enterprise Edition (Java EE) web applications, understanding the purpose and function of specific directories is crucial for developers. One such directory, WEB-INF, plays a pivotal role in the structure and security of your application. Often misunderstood, the WEB-INF directory acts as a secure container, housing essential configuration files, compiled classes, libraries, and other resources that are vital for the proper functioning of the web application. Without a clear grasp of what WEB-INF is used for, developers may encounter deployment issues, security vulnerabilities, and overall instability within their applications. This article will delve deep into the intricacies of the WEB-INF directory, exploring its key components, security features, and best practices for effective utilization. We’ll uncover why it’s a cornerstone of Java EE web application development and how to leverage it for robust and maintainable software.

Understanding the Role of WEB-INF in Java EE

The WEB-INF directory is a special directory within a Java EE web application’s structure. It is not part of the public document tree, meaning clients cannot directly access its contents via a URL. This inherent protection is fundamental to the security of your web application. Within WEB-INF, you’ll typically find critical configuration files like web.xml (the deployment descriptor), compiled Java classes, JAR files containing libraries, and other resources necessary for the application to run. The servlet container, such as Tomcat or Jetty, uses the contents of WEB-INF to manage and configure the web application. The existence of this directory is mandated by the Servlet specification, ensuring consistent behavior across different application servers. Think of it as the engine room of your web application – essential but hidden from public view.

The key purpose of the WEB-INF directory is to encapsulate and protect sensitive application resources. Configuration files, like web.xml, define how the application should behave, including servlet mappings, security constraints, and initialization parameters. Compiled Java classes, stored within the classes subdirectory, contain the executable code of your application. Libraries, packaged as JAR files in the lib subdirectory, provide reusable components and functionalities. By keeping these resources within WEB-INF, you prevent unauthorized access and ensure that they are loaded and managed correctly by the servlet container. This separation of concerns is crucial for maintaining the integrity and security of your web application. According to a report by OWASP, improper configuration is a major source of vulnerabilities in web applications, highlighting the importance of correctly managing the WEB-INF directory and its contents. OWASP Top Ten provides further information on web application vulnerabilities.

Consider a real-world example: a banking application. The WEB-INF directory would contain the configuration files that define how user authentication is handled, the database connection settings, and the compiled code responsible for processing transactions. Placing these sensitive components outside of WEB-INF would expose them to potential attackers, allowing them to bypass security measures and compromise the entire application. The secure nature of WEB-INF is, therefore, a fundamental requirement for building robust and secure web applications. Correctly configuring and utilizing the WEB-INF directory is a best practice endorsed by Java EE experts and is essential for complying with security standards. Learn more about web application development.

Key Components Inside the WEB-INF Directory

The WEB-INF directory typically contains several key subdirectories and files, each serving a specific purpose:

  • web.xml (Deployment Descriptor): This XML file defines the structure and configuration of the web application. It specifies servlets, filters, listeners, security constraints, and other essential settings.
  • classes: This subdirectory contains compiled Java class files, organized in a package-like directory structure. It holds the executable code of your application’s servlets, JSPs, and other Java components.
  • lib: This subdirectory contains JAR files, which are archives of pre-compiled Java code (libraries) that your application depends on. These libraries provide reusable components and functionalities.
  • tags: (Optional) This subdirectory holds tag library descriptor (TLD) files, which define custom JSP tags used in the application’s view layer.

The web.xml file, also known as the deployment descriptor, is arguably the most important file within the WEB-INF directory. It dictates how the servlet container handles requests, maps URLs to servlets, defines security roles, and configures various aspects of the web application. Without a properly configured web.xml, the application will likely fail to deploy or function correctly. Modern Java EE applications are increasingly using annotations to reduce the reliance on web.xml, but the file remains relevant for specifying global configurations and overriding annotation-based settings. For example, you can use web.xml to define a welcome file list, specifying the default pages that should be displayed when a user accesses the application’s root URL.

The classes and lib subdirectories are essential for organizing and managing the application’s code and dependencies. The classes directory holds the compiled Java code that you write specifically for your application, while the lib directory contains external libraries that provide pre-built functionalities. By separating your code from external dependencies, you can improve the maintainability and reusability of your application. Using a build tool like Maven or Gradle can greatly simplify the process of managing dependencies and organizing the lib directory. These tools automatically download and manage the required JAR files, ensuring that your application has all the necessary libraries at runtime.

Security Considerations for the WEB-INF Directory

One of the primary reasons for the existence of the WEB-INF directory is security. By preventing direct access to its contents, the servlet container protects sensitive configuration files and code from unauthorized access. This is a crucial security measure that helps to prevent attackers from gaining access to critical information about your application. Direct access is disabled by the servlet container, so any attempt to browse directly to files under the WEB-INF directory will result in an error (typically a 403 Forbidden error). This is a fundamental security feature built into the Java EE specification.

Here’s a featured snippet optimized paragraph explaining the security aspects:

The WEB-INF directory enhances security by restricting direct client access to sensitive resources such as configuration files (web.xml), compiled Java classes, and libraries (JAR files). This restriction prevents unauthorized users from accessing critical application information or exploiting potential vulnerabilities within the code. By encapsulating these resources, the servlet container ensures that they are only accessed and managed through controlled server-side processes, significantly reducing the risk of security breaches.

However, simply placing files within the WEB-INF directory is not a guarantee of complete security. It’s still important to follow best practices for securing your application code and configuration files. This includes using strong passwords, validating user input, preventing SQL injection attacks, and regularly updating your libraries to patch security vulnerabilities. Tools like static code analyzers can help you identify potential security flaws in your code. Furthermore, it is crucial to ensure that the server itself is properly configured and secured, as vulnerabilities at the server level can still compromise the application, regardless of the security measures implemented within the WEB-INF directory. Acunetix’s Java Security Best Practices offers detailed insights into securing Java applications.

Best Practices for Using WEB-INF

To effectively utilize the WEB-INF directory and ensure the smooth operation of your Java EE web application, consider the following best practices:

  1. Keep the web.xml file clean and organized: Use comments to document the purpose of each configuration element and avoid unnecessary complexity.
  2. Organize your classes and libraries logically: Use meaningful package names and directory structures to improve code maintainability.
  3. Use a build tool like Maven or Gradle: These tools simplify dependency management and automate the build process.
  4. Regularly update your libraries: Keep your dependencies up-to-date to patch security vulnerabilities and benefit from new features.
  5. Secure your configuration files: Use appropriate access control mechanisms to protect sensitive information.

Properly structuring your classes directory is crucial for maintaining a well-organized and manageable codebase. Follow Java’s package naming conventions to create a hierarchical directory structure that reflects the logical organization of your application. For example, if you have a class named com.example.MyServlet, it should be located in the WEB-INF/classes/com/example/ directory. This makes it easier to locate and understand the purpose of different classes within your application. Consistent naming conventions and a clear directory structure will significantly improve code readability and maintainability.

Furthermore, consider using a version control system like Git to track changes to your WEB-INF directory and its contents. This allows you to easily revert to previous versions if necessary and collaborate effectively with other developers. Regularly committing your changes to a remote repository provides a backup of your configuration files and code, protecting you from data loss in case of hardware failures or other unforeseen events. Version control is an indispensable tool for any software development project, and it is especially important for managing the critical configuration files and code within the WEB-INF directory. Atlassian’s Git Tutorials provide comprehensive guidance on using Git for version control.

Infographic here
FAQ About WEB-INF -----------------
What happens if I try to access a file directly in WEB-INF?
The servlet container will return a 403 Forbidden error, preventing direct access to the file.
Can I put HTML files inside WEB-INF?
While you can, it's generally not recommended. HTML files are typically part of the public document root. Putting them in WEB-INF means they can only be served by a servlet, adding unnecessary complexity. Instead, consider using JSPs or a templating engine.
Is WEB-INF directory case-sensitive?
Yes, on most systems (especially Linux/Unix-based), the directory name is case-sensitive. Make sure you use "WEB-INF" exactly.
Understanding the **WEB-INF directory** is more than just a technical detail; it's a fundamental aspect of building secure and maintainable Java EE web applications. We've explored its role as a secure container, the key components it houses, and the best practices for its utilization. Remembering these principles will set you on the path to creating robust and well-structured web applications. Take the time to review your existing projects and ensure that your `WEB-INF` directory is properly configured and secured. Consider exploring other Java EE concepts, such as servlets, JSPs, and dependency injection, to further enhance your understanding of web application development.

Question & Answer :
I’m working on a Java EE web application with the following source code directory structure:

src/main/java <-- multiple packages containing Java classes src/test/java <-- multiple packages containing JUnit tests src/main/resources <-- includes properties files for textual messages src/main/webapp/resources <-- includes CSS, images and all Javascript files src/main/webapp/WEB-INF src/main/webapp/WEB-INF/tags src/main/webapp/WEB-INF/views 

The folder I’m interested in is WEB-INF: It contains web.xml, XML files for setting up servlets, Spring bean wiring contexts and JSP tags and views. I’m trying to understand what constrains/defines this structure. E.g. do JSP files always have to be within WEB-INF or could they be somewhere else? And is there anything else that might go in WEB-INF? Wikipedia’s WAR files entry mentions classes for Java classes and lib for JAR files - not sure I’ve fully grasped when these would be needed in addition to the other source file locations.

The Servlet 2.4 specification says this about WEB-INF (page 70):

A special directory exists within the application hierarchy named WEB-INF. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext, and may be exposed using the RequestDispatcher calls.

This means that WEB-INF resources are accessible to the resource loader of your Web-Application and not directly visible for the public.

This is why a lot of projects put their resources like JSP files, JARs/libraries and their own class files or property files or any other sensitive information in the WEB-INF folder. Otherwise they would be accessible by using a simple static URL (usefull to load CSS or Javascript for instance).

Your JSP files can be anywhere though from a technical perspective. For instance in Spring you can configure them to be in WEB-INF explicitly:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" > </bean> 

The WEB-INF/classes and WEB-INF/lib folders mentioned in Wikipedia’s WAR files article are examples of folders required by the Servlet specification at runtime.

It is important to make the difference between the structure of a project and the structure of the resulting WAR file.

The structure of the project will in some cases partially reflect the structure of the WAR file (for static resources such as JSP files or HTML and JavaScript files, but this is not always the case.

The transition from the project structure into the resulting WAR file is done by a build process.

While you are usually free to design your own build process, nowadays most people will use a standardized approach such as Apache Maven. Among other things Maven defines defaults for which resources in the project structure map to what resources in the resulting artifact (the resulting artifact is the WAR file in this case). In some cases the mapping consists of a plain copy process in other cases the mapping process includes a transformation, such as filtering or compiling and others.

One example: The WEB-INF/classes folder will later contain all compiled java classes and resources (src/main/java and src/main/resources) that need to be loaded by the Classloader to start the application.

Another example: The WEB-INF/lib folder will later contain all jar files needed by the application. In a maven project the dependencies are managed for you and maven automatically copies the needed jar files to the WEB-INF/lib folder for you. That explains why you don’t have a lib folder in a maven project.