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.
Best Answer
The Servlet 2.4 specification says this about WEB-INF (page 70):
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:The
WEB-INF/classes
andWEB-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
andsrc/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 theWEB-INF/lib
folder for you. That explains why you don't have alib
folder in a maven project.