Tech and Media Labs
This site uses cookies to improve the user experience.




Java Web App Directory Layout

Jakob Jenkov
Last update: 2014-06-06

In order to get a Java web server or servlet container to run your Java web application, you need to package the resources inside it (servlets, JSP's etc.) in a standardized way. This text will show you how.

Here are the directories and files a web app should include:

  • MyWebApp (dir)
    • WEB-INF (dir)
      • web.xml
      • classes (dir)
      • lib (dir)
    • index.jsp

The Root Directory

The root directory of you web application can have pretty much any name you want. In the above example the root directory is called MyWebApp.

Underneath the root directory, you can put all files that should be accessible in your web application. For instance, if your web application is mapped to the URL

http://mydomain.com/myWebApp/

... then the index.jsp page will be accessible at the URL

http://mydomain.com/myWebApp/index.jsp    

If you create any subdirectories under the root directory, and place files in these subdirectories, these will be available by the subdirectory/file path, in the web application. For instance, if you create a subdirectory called layout, and put a file inside it called theLayout.jsp, then you could access that file from the outside, via this URL:

http://mydomain.com/myWebApp/layout/theLayout.jsp

The WEB-INF subdirectory is an exception from this rule, as I will describe below.

The WEB-INF Directory

The WEB-INF directory is located just below the web app root directory. This directory is a meta information directory. Files stored inhere are not supposed to be accessible from a browser (although your web app can access them internally, in your code).

Inside the WEB-INF directory there are two important directories (classes and lib, and one important file (web.xml). These are described below.

web.xml

The web.xml file contains information about the web application, which is used by the Java web server / servlet container in order to properly deploy and execute the web application. For instance, the web.xml contains information about which servlets a web application should deploy, and what URL's they should be mapped to. I will not get into more detail about the web.xml file here. Just know, that it exists.

classes

The classes directory contains all compiled Java classes that are part of your web application. The classes should be located in a directory structure matching their package structure, just like if you were to run them directly from the commandline, or package them in a JAR file.

lib

The lib directory contains all JAR files used by your web application. This directory most often contains any third party libraries that your application is using. You could, however, also put your own classes into a JAR file, and locate it here, rather than putting those classes in the classes directory.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC