Web Applications

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.

Web Application Files and Directory Structure

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:

  • 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

  • /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

The Web Application Deployment Descriptor

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.

Listing 12.7. Deployment Descriptor for a Simple Web Application
<?xml version="1.0" encoding="UTF-8"?>
<Web-app version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee
/Web-app_2_4.xsd">
  <display-name>examples</display-name>
  <servlet>
    <servlet-name>VerifyData</servlet-name>
    <servlet-class>VerifyData</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>HTMLPage</servlet-name>
    <servlet-class>HTMLPage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>VerifyData</servlet-name>
    <url-pattern>/verifydata</url-pattern>
  </servlet-mapping>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>HTMLPage</servlet-name>
    <url-pattern>/htmlpage</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/error404.html</location>
  </error-page>
  <jsp-config/>
</Web-app>

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

Table 12.4. XML Tags Used in Listing 12.8
XML TagDescription
Web-appRoot for the deployment descriptor.
display-nameShort name for application, need not be unique.
servlet-nameThe official name of the servlet, must be unique.
servlet-classThe servlet's fully-qualified classname.
init-paramInitialization parameters available to the servlet. This is followed by name/value pairs of parameters.
param-nameThe name of the parameter.
param-valueThe value for the parameter.
servlet-mappingUsed to map a servlet to a URL.
url-patternPatterns that will map onto this servlet.
session-configDefines timeout behavior for sessions.
welcome-file-listDefines a list of files that will be displayed if the user supplied URL denotes a directory rather than an explicit file.
error-pageUsed 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.

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

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