PrevBeginningNext API

Web Modules

In the J2EE architecture, Web components and static Web content files such as images are called Web resources. A Web module is the smallest deployable and usable unit of Web resources. A J2EE Web module corresponds to a Web application as defined in the Java Servlet specification.

In addition to Web components and Web resources, a Web module can contain other files:

A Web module has a specific structure. The top-level directory of a Web module is the document root of the application. The document root is where JSP pages, client-side classes and archives, and static Web resources, such as images, are stored.

The document root contains a subdirectory named /WEB-INF/, which contains the following files and directories:

You can also create application-specific subdirectories (that is, package directories) in either the document root or the /WEB-INF/classes/ directory.

A Web module can be deployed as an unpacked file structure or can be packaged in a JAR file known as a Web archive (WAR) file. Because the contents and use of WAR files differ from those of JAR files, WAR file names use a .war extension. The Web module just described is portable; you can deploy it into any Web container that conforms to the Java Servlet Specification.

To deploy a WAR on the Application Server, the file must also contain a runtime deployment descriptor. The runtime deployment descriptor is an XML file that contains information such as the context root of the Web application and the mapping of the portable names of an application's resources to the Application Server's resources. The Application Server Web application runtime DD is named sun-web.xml and is located in /WEB-INF/ along with the Web application DD. The structure of a Web module that can be deployed on the Application Server is shown in Figure 3-5.

New Greeting

Figure 3-5 Web Module Structure

Packaging Web Modules

A Web module must be packaged into a WAR in certain deployment scenarios and whenever you want to distribute the Web module. You package a Web module into a WAR using the Application Server deploytool utility, by executing the jar command in a directory laid out in the format of a Web module, or by using the asant utility. This tutorial allows you to use use either the first or the third approach. To build the hello1 application, follow these steps:

  1. In a terminal window, go to <INSTALL>/j2eetutorial14/examples/web/hello1/.
  2. Run asant build. This target will spawn any necessary compilations and will copy files to the <INSTALL>/j2eetutorial14/examples/web/hello1/build/ directory.

To package the application into a WAR named hello1.war using asant, use the following command:

asant create-war 

This command uses web.xml and sun-web.xml files in the <INSTALL>/j2eetutorial14/examples/web/hello1 directory.

To learn how to configure this Web application, package the application using deploytool by following these steps:

  1. Start deploytool.
  2. Create a Web application called hello1 by running the New Web Component wizard. Select FileRight ArrowNewRight ArrowWeb Component.
  3. In the New Web Component wizard:
    1. Select the Create New Stand-Alone WAR Module radio button.
    2. In the WAR Location field, enter <INSTALL>/j2eetutorial14/examples/web/hello1/hello1.war.
    3. In the WAR Name field, enter hello1.
    4. Click Edit Contents to add the content files.
    5. In the Edit Contents dialog box, navigate to <INSTALL>/j2eetutorial14/examples/web/hello1/build/. Select duke.waving.gif, index.jsp, and response.jsp and click Add. Click OK.
    6. Click Next.
    7. Select the No Component radio button.
    8. Click Next.
    9. Click Finish.
  4. Select FileRight ArrowSave.

A sample hello1.war is provided in <INSTALL>/j2eetutorial14/examples/web/provided-wars/. To open this WAR with deploytool, follow these steps:

  1. Select FileRight ArrowOpen.
  2. Navigate to the provided-wars directory.
  3. Select the WAR.
  4. Click Open Module.

Deploying Web Modules

You can deploy a Web module to the Application Server in several ways:

All these methods are described briefly in this chapter; however, throughout the tutorial, we use deploytool or asant for packaging and deploying.

Setting the Context Root

A context root identifies a Web application in a J2EE server. You specify the context root when you deploy a Web module. A context root must start with a forward slash (/) and end with a string.

In a packaged Web module for deployment on the Application Server, the context root is stored in sun-web.xml. If you package the Web application with deploytool, then sun-web.xml is created automatically.

Deploying an Unpackaged Web Module

It is possible to deploy a Web module without packaging it into a WAR. The advantage of this approach is that you do not need to rebuild the package every time you update a file contained in the Web module. In addition, the Application Server automatically detects updates to JSP pages, so you don't even have to redeploy the Web module when they change.

However, to deploy an unpackaged Web module, you must create the Web module directory structure and provide the Web application deployment descriptor web.xml. Because this tutorial uses deploytool for generating deployment descriptors, it does not document how to develop descriptors from scratch. You can view the structure of deployment descriptors in three ways:

Since you explicitly specify the context root when you deploy an unpackaged Web module, usually it is not necessary to provide sun-web.xml.

Deploying with the Admin Console
  1. Expand the Applications node.
  2. Select the Web Applications node.
  3. Click the Deploy button.
  4. Select the No radio button next to Upload File.
  5. Type the full path to the Web module directory in the File or Directory field. Although the GUI gives you the choice to browse to the directory, this option applies only to deploying a packaged WAR.
  6. Click Next.
  7. Type the application name.
  8. Type the context root.
  9. Select the Enabled box.
  10. Click the OK button.
Deploying with asadmin

To deploy an unpackaged Web module with asadmin, open a terminal window or command prompt and execute

asadmin deploydir full-path-to-web-module-directory 

The build task for the hello1 application creates a build directory (including web.xml) in the structure of a Web module. To deploy hello1 using asadmin deploydir, execute:

asadmin deploydir --contextroot /hello1

After you deploy the hello1 application, you can run the Web application by pointing a browser at


You should see the greeting form depicted earlier in Figure 3-3.

A Web module is executed when a Web browser references a URL that contains the Web module's context root. Because no Web component appears in http://localhost:8080/hello1/, the Web container executes the default component, index.jsp. The section Mapping URLs to Web Components describes how to specify Web components in a URL.

Deploying a Packaged Web Module

If you have deployed the hello1 application, before proceeding with this section, undeploy the application by following one of the procedures described in Undeploying Web Modules.

Deploying with deploytool

To deploy the hello1 Web module with deploytool:

  1. Select the hello1 WAR you created in Packaging Web Modules.
  2. Select the General tab.
  3. Type /hello1 in the Context Root field.
  4. Select FileRight ArrowSave.
  5. Select ToolsRight ArrowDeploy.
  6. Click OK.

You can use one of the following methods to deploy the WAR you packaged with deploytool, or one of the WARs contained in <INSTALL>/j2eetutorial14/examples/web/provided-wars/.

Deploying with the Admin Console
  1. Expand the Applications node.
  2. Select the Web Applications node.
  3. Click the Deploy button.
  4. Select the No radio button next to Upload File.
  5. Type the full path to the WAR file (or click on Browse to find it), and then click the OK button.
  6. Click Next.
  7. Type the application name.
  8. Type the context root.
  9. Select the Enabled box.
  10. Click the OK button.
Deploying with asadmin

To deploy a WAR with asadmin, open a terminal window or command prompt and execute

asadmin deploy full-path-to-war-file 
Deploying with asant

To deploy a WAR with asant, open a terminal window or command prompt in the directory where you built and packaged the WAR, and execute

asant deploy-war 

Listing Deployed Web Modules

The Application Server provides three ways to view the deployed Web modules:

Updating Web Modules

A typical iterative development cycle involves deploying a Web module and then making changes to the application components. To update a deployed Web module, you must do the following:

  1. Recompile any modified classes.
  2. If you have deployed a packaged Web module, update any modified components in the WAR.
  3. Redeploy the module.
  4. Reload the URL in the client.

Updating an Unpackaged Web Module

To update an unpackaged Web module using either of the methods discussed in Deploying an Unpackaged Web Module, reexecute the deploydir operation. If you have changed only JSP pages in the Web module directory, you do not have to redeploy; simply reload the URL in the client.

Updating a Packaged Web Module

This section describes how to update the hello1 Web module that you packaged with deploytool.

First, change the greeting in the file <INSTALL>/j2eetutorial14/examples/web/hello1/web/index.jsp to

<h2>Hi, my name is Duke. What's yours?</h2> 

Run asant build to copy the modified JSP page into the build directory. To update the Web module using deploytool follow these steps:

  1. Select the hello1 WAR.
  2. Select ToolsRight ArrowUpdate Module Files. A popup dialog box will display the modified file. Click OK.
  3. Select ToolsRight ArrowDeploy. A popup dialog box will query whether you want to redeploy. Click Yes.
  4. Click OK.

To view the modified module, reload the URL in the browser.

You should see the screen in Figure 3-6 in the browser.

New Greeting

Figure 3-6 New Greeting

Dynamic Reloading

If dynamic reloading is enabled, you do not have to redeploy an application or module when you change its code or deployment descriptors. All you have to do is copy the changed JSP or class files into the deployment directory for the application or module. The deployment directory for a Web module named context_root is <J2EE_HOME>/domains/domain1/applications/j2ee-modules/context_root. The server checks for changes periodically and redeploys the application, automatically and dynamically, with the changes.

This capability is useful in a development environment, because it allows code changes to be tested quickly. Dynamic reloading is not recommended for a production environment, however, because it may degrade performance. In addition, whenever a reload is done, the sessions at that time become invalid and the client must restart the session.

To enable dynamic reloading, use the Admin Console:

  1. Select the Applications node.
  2. Check the Reload Enabled box to enable dynamic reloading.
  3. Enter a number of seconds in the Reload Poll Interval field to set the interval at which applications and modules are checked for code changes and dynamically reloaded.
  4. Click the Save button.

In addition, to load new servlet files or reload deployment descriptor changes, you must do the following:

  1. Create an empty file named .reload at the root of the module:
  2. <J2EE_HOME>/domains/domain1/applications/j2ee-modules/context_root/.reload

  3. Explicitly update the .reload file's time stamp each time you make these changes. On UNIX, execute
  4. touch .reload

For JSP pages, changes are reloaded automatically at a frequency set in the Reload Pool Interval. To disable dynamic reloading of JSP pages, set the reload-interval property to -1.

Undeploying Web Modules

You can undeploy Web modules in four ways:

PrevBeginningNext API


All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.