Download
FAQ History |
API
Search Feedback |
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:
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 componentstags
: 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 classesYou 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 thejar
command in a directory laid out in the format of a Web module, or by using theasant
war
task. This tutorial uses the first approach. To build and package thehello1
application into a WAR namedhello1.war
, follow these steps:
- In a terminal window, go to
<
INSTALL
>/j2eetutorial14/examples/web/hello1/
.- Run
asant
build
. This target will spawn any necessary compilations and will copy files to the<
INSTALL
>/j2eetutorial14/examples/web/hello1/build/
directory.- Start
deploytool
.- Create a Web application called
hello1
by running the New Web Component wizard. Select FileNewWeb Component.- In the New Web Component wizard:
- Select the Create New Stand-Alone WAR Module radio button.
- In the WAR Location field, enter
<
INSTALL
>/j2eetutorial14/examples/web/hello1/hello1.war
.- In the WAR Name field, enter
hello1
.- Click Edit Contents to add the content files.
- In the Edit Contents dialog box, navigate to
<
INSTALL
>/j2eetutorial14/examples/web/hello1/build/
. Selectduke.waving.gif
,index.jsp
, andresponse.jsp
and click Add. Click OK.- Click Next.
- Select the No Component radio button.
- Click Next.
- Click Finish.
- Select FileSave.
A sample
hello1.war
is provided in<
INSTALL
>/j2eetutorial14/examples/web/provided-wars/
. To open this WAR withdeploytool
, follow these steps: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
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 withdeploytool
, thensun-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 usesdeploytool
for generating deployment descriptors, it does not document how to develop descriptors from scratch. You can view the structure of deployment descriptors in two ways:Since you explicitly specify the context root when you deploy and unpackaged Web module, usually it is not necessary to provide
sun-web.xml
.Deploying with the Admin Console
- Expand the Applications node.
- Select the Web Applications node.
- Click the Deploy button.
- Select the No radio button next to Upload File.
- 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.
- Click Next.
- Type the application name.
- Type the context root.
- Select the Enabled box.
- Click the OK button.
Deploying with asadmin
To deploy an unpackaged Web module with
asadmin
, open a terminal window or command prompt and executeThe
build
task for thehello1
application creates abuild
directory (includingweb.xml
) in the structure of a Web module. To deployhello1
usingasadmin deploydir
, execute:After you deploy the
hello1
application, you can run the Web application by pointing a browser atYou 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 withdeploytool
:
- Select the
hello1
WAR you created in Packaging Web Modules.- Select the General tab.
- Type
/hello1 i
n the Context Root field.- Select FileSave.
- Select ToolsDeploy.
- 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
- Expand the Applications node.
- Select the Web Applications node.
- Click the Deploy button.
- Select the No radio button next to Upload File.
- Type the full path to the WAR file (or click on Browse to find it), and then click the OK button.
- Click Next.
- Type the application name.
- Type the context root.
- Select the Enabled box.
- Click the OK button.
Deploying with asadmin
To deploy a WAR with
asadmin
, open a terminal window or command prompt and executeListing 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:
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 withdeploytool
.First, change the greeting in the file
<
INSTALL
>/j2eetutorial14/examples/web/hello1/web/index.jsp
toRun
asant
build
to copy the modified JSP page into thebuild
directory. To update the Web module usingdeploytool
follow these steps:To view the modified module, reload the URL in the browser.
You should see the screen in Figure 3-6 in the browser.
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:
In addition, to load new servlet files or reload deployment descriptor changes, you must do the following:
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 three ways:
Download
FAQ History |
API
Search Feedback |
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.