Web applications are, as the name implies, applications run on a Web server. A Web application is simply a set of static HTML pages, servlets, support classes, applets, JavaBeans, and any other resources that are bundled together to form a complete application. The J2EE RI deploytool packages Web applications for you and stores them in a Web Archive Format (WAR) file so they can be easily distributed to Web servers. You can manually create WAR files using the J2SE jar utility, or you can use custom ant (or asant) build files to automate the process.
The Java servlet 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 correct directory structure for you.
Each Web application has a root called the context path (you saw the use of the context path when you deployed and accessed the sample servlets in today's lesson). No two applications, in the same Web server, can have the same context path because this would cause URL conflicts.
Below the document root is a special directory called WEB-INF. The contents of the WEB-INF directory are as follows:
/classes directory— Used to store the class files for the servlets and utility classes
/tags directory— Used to store tag libraries as discussed on Day 14, “JSP Tag Libraries.”
There may also be a META-INF directory that contains the implementation-specific deployment descriptor and a wsdl directory which is discussed on Day 20, “Using RPC-Style Web Services with J2EE.”
For a small application created in the J2EE RI, the directory structure might look like the following:
/META-INF/MANIFEST.MF /WEB-INF/classes/HTMLPage.class /WEB-INF/classes/VerifyData.class /WEB-INF/Web.xml /WEB-INF/sun-Web.xml /examples.css /index.html /VerifyForm.html
Listing 12.7 is the Web.xml or deployment descriptor for a simple Web application. This example is based on the Servlet 2.4 specification.
The deployment descriptor begins with the <?xml> tag that defines the version of XML and the character encoding. This is followed by the document root tag, <Web-app>, that references the XML Schema for validating the document structure (XML Schemas and validating XML documents are covered in Day 16, “Integrating XML with J2EE”). The remainder of the XML describes this particular Web application and sets initialization parameters for the servlets.
Table 12.4 contains a short description of the tags used in this deployment descriptor; see the servlet specification for a more complete list.
XML Tag | Description |
---|---|
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. |
url-pattern | Patterns that will map onto this servlet. |
session-config | Defines timeout behavior for sessions. |
welcome-file-list | Defines a list of files that will be displayed if the user supplied URL denotes a directory rather than an explicit file. |
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.
To be validated, the deployment descriptor requires an XML schema or a Document Type Definition (DTD) file. The XML Schema (or DTD) contains the rules for processing the deployment descriptor and is fully described in the servlet specification.
If you are writing your own deployment descriptor, you will need to follow the XML Schema rules to ensure that it is valid; otherwise, your application will not deploy.
3.135.247.181