06 - Web Application Structure

6.1 Overview

In this chapter we will discuss the typical directory structure of a web application and its deployment.

6.2 Web Application Directory Structure

In any web application we can have following components –

  • Static contents like HTML.
  • Client side files like CSS and Javascript.
  • JSP (Java Server Pages) to generate dynamic content.
  • Servlets.
  • External library or jar files.
  • Any other java utility classes.
  • web.xml also known as deployment descriptor

All web servers and container expects the web application to be available in a specific directory structure. Below is the typical directory structure

Details –

Web Application Root Directory – This is the main or Root folder of web application. Usually name of this folder becomes your web application context. For example ,if our web application name is FirstWebApplication , then folder name will be FirstWebApplication and web application will be accessible via http://localhost:8080/FirstWebApplication

WEB-INF- This is the special directory under web application root directory. This is special because this is secured folder and files available within this folder will not be accessible to client directly. Which means say if this directory has one file “index.html” then this file cannot be accessed directly via http://localhost:8080/FirstWebApplication/index.html

WEB-INF/lib- All the required jar files or third party jar files will be placed inside this directory.

WEB-INF/classes- All the java code including servlets for the web application will go inside classes folder. We can also put our own classes into a JAR file, and place it here, rather than putting those classes in the classes directory.

web.xml – web.xml resides under WEB-INF folder and it is also known as deployment descriptor. All the configuration of web application like servlets configuration , filters configuration , welcome file list etc are configured in web.xml. We will discuss web.xml in detail

META-INF/MAINFEST.MF- This is the manifest file

Static Files – All static files like HTML, css ,javascript will be placed directly under web application root directory. If we want to make these files secure , we need to place these files under WEB-INF directory.

 

6.3 Web.xml (Deployment Descriptor)

This is the standard file for any web application and container comes to know about all the detail of web application through this file only.

All configurations of all components of a web application are done in web.xml and it is placed directly under WEB-INF folder.

6.3.1 Servlet Mapping

 All servlets mapping need to be done in web.xml so that servlet container will be aware of the servlets and also to make it accessible from a browser. We must tell the servlet container what servlets to deploy, and what URL's to map the servlets to.

Below web.xml configures the Servlet “MyFirstServlet” with two init param.

<?xml version="1.0" encoding="ISO-8859-1"?>     
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"     version="2.4">
  <servlet>
    <servlet-name> MyFirstServlet </servlet-name>
    <servlet-class> MyFirstServlet </servlet-class>
   <init-param>
     <param-name>name</param-name>
     <param-value>First Servlet</param-value>
 </init-param>
   <init-param>
     <param-name>purpose</param-name>
     <param-value>learning</param-value>
 </init-param>               
  </servlet>                   
  <servlet-mapping>
    <servlet-name> MyFirstServlet </servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>  

Servlet is configured using the <servlet> element where we assign the servlet a unique name, and writes the qualified class of the servlet. Then map servlet to a specific URL or a URL pattern. This is done in the <servlet-mapping> element. In the above example, all URL's ending in .html are sent to the servlet.

In above sample we have defined one init param as well which can be accessed in a servlet. As the init params are defined within a servlet tag, they are limited to that servlet only. One init-param tag is needed for one init parameter.

The * is a wild card, meaning any text.

When a request with URL ending with “html” , container-

  1. check the servlet mapping of web.xml and finds the servlet name based on url pattern.
  2. Once servlet name is found, gets the servlet class for matching servlet name.
  3. Delegate request to the servlet class configured in corresponding servlet-class tag.

6.3.2 Context Parameters

 init parameters are limited to the servlet in which they are defined but context parameters are available to all the servlets of the web application. Below is the sample configuration of context parameter.

Since these parameters are not limited to any servlet, they are defined outside the <servlet> tag

<context-param>
    <param-name>param</param-name>
    <param-value>the value</param-value>
</context-param>

 

6.3.3 Early servlet loading & Initialization

As discussed earlier, servlet is loaded when first request for that servlet comes. There are scenarios where we need to get it loaded even before that or we can say at the time of application deployment. To do that, we can set the <load-on-startup> property of servlet configuration in web.xml

If we do not specify a <load-on-startup> element, the servlet container will typically load your servlet when the first request arrives for it.By setting a <load-on-startup> element, we tell the servlet container to load the servlet as soon as the servlet container starts.

<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>MyFirstServlet</servlet-class>  
<init-param>
<param-name>param-name</param-name>
<param-value>param-value</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

The number inside the <load-on-startup>1</load-on-startup> tells the servlet container in what sequence the servlets should be loaded. The lower numbers are loaded first. If the value is negative, or unspecified, the servlet container can load the servlet at any time.

6.4 Web Archive (WAR)

Once the web application is developed, it needs to be deployed on server. Sun specified a format known as WAR (web achieve) in which the web application has to be packaged.

Once the packaged war file is deployed to the server, it gets exploded and deploys to the server.

We can create a war file out of web application by executing below command using command prompt.(we need to run this command after navigating to the web application root directory)

jar cvf myapplication.war .

Above command says generate a war with name “myapplication.war” in a current directory (there is a . in the end of command)

Like us on Facebook