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.
WEB-INF. This directory, which is contained within the Document Root, is invisible from the web container. It contains all resources needed to run the application, from Java classes, to JAR files and libraries, to other supporting files that the developer does not want a web user to access.
The META-INF directory is private and can't be accessed from the outside. On the other hand, it also contains the WEB-INF public directory with all the static web resources, including HTML pages, images, and JS files. Moreover, it contains the web. xml file, servlet classes, and libraries.
/WEB-INF/web. xml - The Web Application Deployment Descriptor for your application. This is an XML file describing the servlets and other components that make up your application, along with any initialization parameters and container-managed security constraints that you want the server to enforce for you.
The WEB-INF directory contains the deployment descriptors for the Web application (web. xml and weblogic. xml) and two subdirectories for storing compiled Java classes and library JAR files. These subdirectories are respectively named classes and lib.
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. TheWEB-INF
node is not part of the public document tree of the application. No file contained in theWEB-INF
directory may be served directly to a client by the container. However, the contents of theWEB-INF
directory are visible to servlet code using thegetResource
andgetResourceAsStream
method calls on theServletContext
, and may be exposed using theRequestDispatcher
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.
When you deploy a Java EE web application (using frameworks or not),its structure must follow some requirements/specifications. These specifications come from :
- The servlet container (e.g Tomcat)
- Java Servlet API
- Your application domain
Java Servlet API requirements
Java Servlet API states that your root application directory must have the following structure :
ApplicationName
|
|--META-INF
|--WEB-INF
|_web.xml <-- Here is the configuration file of your web app(where you define servlets, filters, listeners...)
|_classes <--Here goes all the classes of your webapp, following the package structure you defined. Only
|_lib <--Here goes all the libraries (jars) your application need
These requirements are defined by Java Servlet API.
3. Your application domain
Now that you've followed the requirements of the Servlet container(or application server) and the Java Servlet API requirements, you can organize the other parts of your webapp based upon what you need.
- You can put your resources (JSP files, plain text files, script files) in your application root directory. But then, people can access them directly from their browser, instead of their requests being processed by some logic provided by your application. So, to prevent your resources being directly accessed like that, you can put them in the WEB-INF directory, whose contents is only accessible by the server.
-If you use some frameworks, they often use configuration files. Most of these frameworks (struts, spring, hibernate) require you to put their configuration files in the classpath (the "classes" directory).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With