Download PDF Home History |  | API Search Feedback FAQ

> ## 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:

> - Server-side utility classes (database beans, shopping carts, and so on). Often these classes conform to the JavaBeans component architecture.
> - Client-side classes (applets and utility classes).

> 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:

> - `web.xml`: The Web application deployment descriptor
> - Tag library descriptor files (see Tag Library Descriptors)
> - `classes`: A directory that contains server-side classes: servlets, utility classes, and JavaBeans components
> - `tags`: A directory that contains tag files, which are implementations of tag libraries (see Tag File Location)
> - `lib`: A directory that contains JAR archives of libraries called by server-side classes

> 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.

> 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 FileNewWeb 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 FileSave.

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

> 1. Select FileOpen.
> 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:

> - By pointing the Application Server at an unpackaged Web module directory structure using `asadmin` or the Admin Console.
> - By packaging the Web module and
> - Copying the WAR into the `<``J2EE_HOME``>/domains/domain1/autodeploy/` directory.
> - Using the Admin Console, `asadmin`, `asant`, or d`eploytool` to deploy the WAR.

> 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:

> - In `deploytool`, select ToolsDescriptor ViewerDescriptor Viewer to view `web.xml` and ToolsDescriptor ViewerApplication Server Descriptor to view `sun-web.xml`.
> - Use a text editor to view the `web.xml` and `sun-web.xml` files in the example directories.
> - Unpackage one of the WARs in `<``INSTALL``>/j2eetutorial14/examples/web/provided-wars/` and extract the descriptors.

> 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
>       <INSTALL>/j2eetutorial14/examples/web/hello1/build

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

>     http://localhost:8080/hello1

> 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 i`n the Context Root field.
> 4. Select FileSave.
> 5. Select ToolsDeploy.
> 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:

> - `deploytool`
> 1. Select localhost:4848 from the Servers list.
> 2. View the Deployed Objects list in the General tab.
> - Admin Console
> 1. Open the URL `http://localhost:4848/asadmin` in a browser.
> 2. Expand the nodes ApplicationsWeb Applications.
> - `asadmin`
> 1. Execute

> `asadmin` `list-components`

> ### 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 ToolsUpdate Module Files. A popup dialog box will display the modified file. Click OK.
> 3. Select ToolsDeploy. 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.

> 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:

> `<``J2EE_HOME``>/domains/domain1/applications/j2ee-modules/``context_root``/.reload`

> 2. Explicitly update the `.reload` file's time stamp each time you make these changes. On UNIX, execute

> `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:

> - `deploytool`
> 1. Select localhost:4848 from the Servers list.
> 2. Select the Web module in the Deployed Objects list of the General tab.
> 3. Click the Undeploy button.
> - Admin Console
> 1. Open the URL `http://localhost:4848/asadmin` in a browser.
> 2. Expand the Applications node.
> 3. Select Web Applications.
> 4. Click the checkbox next to the module you wish to undeploy.
> 5. Click the Undeploy button.
> - `asadmin`
> 1. Execute

> `asadmin undeploy` `context_root`

> - `asant`
> 1. In the directory where you built and packaged the WAR, execute

> `asant undeploy-war`

Download PDF Home History |  | API Search Feedback FAQ

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.