Java web application structure using tomcat

To qualify as a web application in j2ee we have some set of rules that has to be there in web application. The application has some folder

The below image shows the folder structure of java based application. Here we will each of them one by one.

java-web-application-structure-using-tomcat_files

Web-application folder

This is your web application name which we are going to develop, you can give any name as you required.

WEB-INF folder

This is the mandatory folder for java web application. The WEB-INF folder has to be there in java based web application. We will keep all the configuration and java classes’ related folder inside WEB-INF folder like classes, lib and other folders.

Classes folder inside WEB-INF

There should be a classes folder inside WEB-INF directory of your web application. The classes folder contains all complied java classes.

Lib folder inside WEB-INF

There should be lib folder inside WEB-INF folder of your web application. All the external dependency jar files for web application should be copied in lib folder.

Src folder inside WEB-INF

The src folder is optional inside WEB-INF. It is not advisable to put any src folder in WEB-INF. Your source should not be the part of WEB-INF folder, Better you keep outside all the java source files and complied files should be copied inside classes folder of WEB-INF folder.

Other folder inside WEB-INF

You can put some other folder in Web-INF folder which can be used to put some common types of files like custom tags tld files, spring configuration file, hibernate configuration and hbm files.

Web.xml inside WEB-INF

Your java based application must have WEB-INF folder and web.xml file must be there in WEB-INF folder. If web.xml is missing your web application will not be register as web application.

Web.xml also called as deployment descriptor.

The web.xml should have web-app as root context. And you can define the other server resource like servlet, filter, and listener in web.xml file.

Below is sample web.xml file for java based web application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!--  For displaying web application name-->
    <display-name>java web application</display-name>
    <!--  Desscription of web application -->
    <description>Test application</description>
    <!--
        Application specific paramater which will be available across the
        application
    -->
    <context-param>
        <description>Test param</description>
        <param-name>name</param-name>
        <param-value>value</param-value>
    </context-param>
    <distributable />
    <env-entry>
        <description></description>
        <env-entry-name></env-entry-name>
        <env-entry-type></env-entry-type>
        <env-entry-value></env-entry-value>
    </env-entry>
    <error-page>
        <error-code>400</error-code>
        <location>/error.jsp</location>
    </error-page>
    <filter>
        <description></description>
        <display-name></display-name>
        <filter-class>test.Myfilter</filter-class>
        <filter-name>myFilter</filter-name>
        <icon></icon>
        <init-param>
            <description></description>
            <param-name></param-name>
            <param-value></param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <dispatcher>REQUEST</dispatcher>
        <filter-name>myFilter</filter-name>
        <servlet-name></servlet-name>
        <url-pattern></url-pattern>
    </filter-mapping>
    <icon></icon>
    <jsp-config>
        <jsp-property-group></jsp-property-group>
 
    </jsp-config>
    <listener>
        <description></description>
        <display-name></display-name>
        <icon></icon>
        <listener-class></listener-class>
    </listener>
    <login-config>
        <auth-method></auth-method>
        <form-login-config>
            <form-login-page></form-login-page>
            <form-error-page></form-error-page>
        </form-login-config>
        <realm-name></realm-name>
    </login-config>
 
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
 
    <session-config></session-config>
    <servlet>
        <init-param>
            <param-name></param-name>
            <param-value></param-value>
        </init-param>
        <servlet-class></servlet-class>
        <servlet-name></servlet-name>
        <load-on-startup>10</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name></servlet-name>
        <url-pattern></url-pattern>
    </servlet-mapping>
    <mime-mapping>
        <extension></extension>
        <mime-type></mime-type>
    </mime-mapping>
</web-app>

Outside WEB-INF folder contains

You can put web resource outside WEB-INF folder like plan html pages, plan text files, jsp files.