In this section, Web applications will be described in more detail—what happens when one is created and what is happening behind the scenes when you use the J2EE RI.
Web applications are run on a Web server. A Web application is simply a set of static HTML pages, servlets, support classes, applets, JavaBeans, and any other resource that bundled together form a complete application. The J2EE RI packages Web applications for you and stores them in a Web Archive Format (WAR) file so they can be easily distributed to other Web servers.
The Java Servlets specification defines a structured hierarchy of directories that are used for deployment and packaging purposes. Using the J2EE RI deploytool, class files and other configuration files are placed in the directory structure for you. If you use a different Web server (such as Jakarta Tomcat), you may need to manually create and place files in the correct place.
Each Web application has a root called the context path. You entered the context path when you deployed your servlets. No two applications can have the same context path because this would cause potential URL conflicts.
The context root is usually mapped onto a physical directory called the document root. Using the J2EE RI, this directory is found in /public_html under the J2EE installation directory. This is also where the WAR file is stored, if there is one.
Below the document root is a special directory called WEB-INF. The contents of the WEB-INF directory are as follows:
If the WAR file exists, there will also be a META-INF directory that contains information useful to Java Archive tools.
For a small application, the directory structure might look like the following:
imagesackground.gif imagescutout.gif index.html META-INFMANIFEST.MF servlets.war VerifyForm.html WEB-INFclassesHTMLPage.class WEB-INFclassesVerifyData.class WEB-INFlibean.jar WEB-INFweb.xml
Listing 12.8 is the deployment descriptor for a simple Web application showing the major features. This example is based on one given in the Servlets 2.3 specification.
It begins with two tags (<?xml> <!DOCTYPE>) that define the XML document structure. The remainder of the XML describes the Web application and sets parameters for the servlets.
Table 12.4 contains a short description of the tags used in this deployment descriptor, see the servlets specification for a more complete list.
XML Tag | Description |
---|---|
DOCTYPE | Indicates that this is a deployment descriptor for version 2.3 of the servlet's specification. |
web-app | Root for the deployment descriptor |
display-name | Short name for application, need not be unique. |
servlet-name | The official name of the servlet, must be unique. |
servlet-class | The servlet's fully-qualified classname. |
init-param | Initialization parameters available to the servlet. This is followed by name/value pairs of parameters. |
param-name | The name of the parameter. |
param-value | The value for the parameter. |
servlet-mapping | Used to map a servlet to a URL. |
session-config | Defines timeout behavior for sessions. |
error-page | Used to map an HTTP error status code onto a Web resource, such as an HTML page, that will be displayed instead of the standard browser error pages (see the “Handling Errors” section later in the chapter). |
If you use the J2EE RI, the deployment descriptor is created for you. If you are using other Web servers, you may need to manually create the XML for the deployment descriptor and save it as the web.xml file.
To be validated, the deployment descriptor requires a Document Type Definition (DTD) file.
The DTD contains the rules for processing the deployment descriptor. If you are comfortable with the syntax used in DTDs, the deployment descriptor DTD itself can be found by following the URL given in the DOCTYPE tag (http://java.sun.com/dtd/web-app_2_3.dtd). Also see Appendix C,“An Overview of XML,” on the CD-ROM.
For more information on DTDs, see also the Servlet Specification for a full description of the deployment descriptor DTD file.
If you are writing your own deployment descriptor, you will need to follow the DTD rules to ensure that it is valid; otherwise, your application will not be deployed.
3.139.240.244