Web Applications

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 Application Files and Directory Structure

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:

  • web.xml file— The Web application deployment descriptor

  • /classes directory— Used to store the class files for the servlets and utility classes

  • /lib directory— Storage area for Java Archive (JAR) files

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
						

The Web Application Deployment Descriptor

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.

Listing 12.8. Deployment Descriptor for a Simple Web Application
 1: <?xml version="1.0" encoding="UTF-8"?>   2: <!DOCTYPE web-app PUBLIC '-//Sun
 Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'>
   3: <web-app>
 4:   <display-name>A Simple Application</display-name>
 5:   <servlet>
 6:     <servlet-name>catalog</servlet-name>
 7:     <servlet-class>CatalogServlet</servlet-class>
 8:     <init-param>
 9:       <param-name>catalog</param-name>
10:       <param-value>Spring</param-value>
11:     </init-param>
12:   </servlet>
13:   <servlet-mapping>
14:     <servlet-name>catalog</servlet-name>
15:     <url-pattern>/catalog/*</url-pattern>
16:   </servlet-mapping>
17:   <session-config>
18:     <session-timeout>30</session-timeout>
19:   </session-config>
20:   <error-page>
21:     <error-code>404</error-code>
22:     <location>/error404.html</location>
23:   </error-page>
24: </web-app>
						

Table 12.4 contains a short description of the tags used in this deployment descriptor, see the servlets specification for a more complete list.

Table 12.4. XML Tags Used in Listing 12.8
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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.175