Under component and container architecture of J2EE, the business logic (typically as EJB’s), contents or presentations (typically as web-tier components such as Servlet and JSP components) are captured in the form of components and those components are then run within corresponding host execution environment called containers. In the web-tier container, the components are constructed in the form of web components and web components can be either in the form of servlet or JSP pages while in the EJB-tier container, the components are constructed in the form of EJB beans.

Web components are in the form of either Servlet or JSP (along with JavaBean’s and custom tags)‏. Web components run in a Web container. Tomcat and Resin are popular web containers.  All J2EE compliant app servers (Sun Java System App Server) provide web containers. Web container provides system services to Web components.

Web Application is a deployable package which contains:

  • Web components (Servlets and JSP’s)‏
  • Static resource files such as images
  • Helper classes
  • Libraries
  • Deployment descriptor (web.xml file)‏

Web Application can be represented as a hierarchy of directories and files (unpacked form) or *.WAR file reflecting the same hierarchy (packed form)‏.

Java Web Application Technologies

Web Application Development and Deployment Steps

  1. Write (and compile) the Web component code (Servlet or JSP) and helper classes referenced by the web component code
  2. Create any static resources (for example, images or HTML pages)
  3. Create deployment descriptor (web.xml)
  4. Build the Web application (*.war file or deployment-ready directory)
  5. Deploy the web application into a Web container

1. Write and compile the Web component code

Create development tree structure. Write either servlet code or JSP pages along with related helper code. Create build.xml for Ant-based build (and other application development life-cycle management) process. Any IDE (e.g. NetBeans) handles all these chores.

In order to facilitate iterative development/deployment and also in order to keep source code separate from compiled files, the source code for  the tutorial examples is stored in the following structure under each application directory, for example, for hello1 web application:

  • build.xml – Ant build file
  • context.xml – Optional application configuration file
  • src – Java source of servlets and JavaBeans components
  • web – JSP pages and HTML pages, images

Example: hello2 Tree Structure (before “ant build”  command)‏


  • src/servlets
    • GreetingServlet.java
    • ResponseServlet.java
  • web
    • WEB-INF
      • ¨web.xml
    • duke.waving.gif
  • build.xml

Above is an example directory structure of hello2 web application you find in the Java. There are two servlet Java source files. And there is a web directory called web which contains WEB-INF directory and other static resource files.

2. Create any static resources

The next step is to create any static resources that will be used by the web components. And they include some static HTML pages or image files that are used by the HTML pages or JSP pages.

  • HTML pages
    • Custom pages
    • Login pages
    • Error pages
  • Image files that are used by HTML pages or JSP pages
    • Example: duke.waving.gif (an example image that is used often in Java-related web pages)

3. Create deployment descriptor (web.xml)

The next step is to create deployment descriptor, web.xml file.  By the way, the name of this file has to be web.xml file. Deployment descriptor contains deployment time runtime instructions to the Web container. URL that the client uses to access the web component. Every web application has to have it

4. Build the Web application

A web application is either in the form or *.war file (packed) or laid out form of the war file (unpacked).  Typically a build process for the web application involves creating “build” directory which functions as the root directory for the compile time generated pieces such as java classes and pieces that are needed to create *.war file such as web.xml file.

Build process is made of

  • create build directory (if it is not present) and its subdirectories
  • compile Java code into build/WEB-INF/classes directory
  • Java classes reside under ./WEB-INF/classes directory
  • copy web.xml file into build/WEB-INF directory
  • copy image files into build directory

5. Deploy Web application

Deploy the application over deployment platform such as Sun Java System App Server or Tomcat. There are 2 ways to deploy to Sun Java System App server:

  • App server admin console
  • NetBeans

6. Perform Client Access to Web Application

From a browser, go to URN of the Web application.

For example, http://localhost:8080/hello2/greeting

Web Application Archive

Web application can be deployed in two different forms:

  • a *.war file or
  • an unpacked directory laid out in the same format as a *.war file (build directory)‏

Use *.war file when you have to deploy on a remote machine

  • As ant deploy-war command

*.war file is a ready-to-deployable package over web container.  It is like *.jar file and obviously contains things that need to be deployed over the container such as web service components (servlets, JSP pages), server side helper or utility classes, static resources, and sometimes applet classes that would be needed by the client.

Document root is a context is a name that gets mapped to the document root of a Web application
  • /hello1 is context for hello1 example
  • Distinguishes a Web application in a single Web container
  • Has to be specified as part of client URN

Directory Structure of *.WAR file

How to Create *.WAR file?

There are 3 different ways to create *.war file:

  • Use IDE (NetBeans)‏
  • Use ant tool after putting proper build instruction in build.xml file. “asant create-war” (under J2EE 1.4 tutorial)‏
  • Use “jar cvf <filename>.war .” command under build directory

WEB-INF Directory

It is subdirectory of Document root. It contains following:

  • web.xml : Web application deployment descriptor
  • JSP tag library descriptor files
  • Classes : A directory that contains server-side classes: servlets, utility classes, and JavaBeans components
  • lib : A directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes)

Configuring Web Application via web.xml

Configuration information is specified in web.xml  (Web Applications Deployment Descriptor). Following is the list of things that can constitute web.xml file.

  • Prolog
  • Alias Paths
  • Context and Initialization Parameters
  • Event Listeners
  • Filter Mappings
  • Error Mappings
  • Reference to Environment Entries, Resource environment entries, or Resources

The most important configuration information that you need to understand is what is called alias paths, which define which URL is mapped to which web components.

Not only elements in web.xml are case sensitive, they are also order-sensitive. Every XML document needs a prolog.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
Alias Path

When a request is received by Servlet container, it must determine which Web component in a which web application should handle the request. It does so by mapping the URL path contained in the request to a Web component

  • A URL path contains the context root and alias path
    • http://<host>:8080/context_root/alias_path
  • Alias Path can be in the form of either
    • /alias-string   (for servlet)
    • or /*.jsp  (for JSP)‏

Example of Alias Path

          <description>no description</description>
          <description>no description</description>

Event Listeners (of web.xml)

Receives servlet life-cycle events


Filter Mappings (of web.xml)

Specify which filters are applied to a request, and in what order


Error Mappings (of web.xml)‏

Maps status code returned in an HTTP response to a Java programming language exception returned by any Web component and a Web resource


References (of web.xml)

Need when web components make references to environment entries, resource environment entries, or resources such as databases. Example: declare a reference to the data source


Example web.xml of hello2

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" 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">
Tagged with: JAVA

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.