Web Applications

 < Day Day Up > 



All Struts applications are packaged using the Java Web application format. Therefore, before we continue, let's take a brief look at Java Web applications.

Java Web applications are best described by the Java Servlet Specification 2.2, which introduced the idea using the following description: "A Web Application is a collection of servlets, HTML pages, classes, and other resources that can be bundled and run on multiple containers from multiple vendors." In simpler terms, a Java Web application is a collection of one or more Web components that have been packaged together for the purpose of creating a complete application to be executed in the Web layer of an enterprise application. Here is a list of the common components that can be packaged in a Web application:

  • Servlets

  • JavaServer Pages (JSPs)

  • JSP custom tag libraries

  • Utility classes and application classes

  • Static documents, including HTML, images, and JavaScript

  • Metainformation describing the Web application

The Directory Structure

All Web applications are packed into a common directory structure, and this directory structure is the container that holds the components of a Web application. The first step in creating a Web application is to create this structure. Table 1.2 describes a sample Web application, named wroxapp, and lists the contents of each of its directories. Each one of these directories will be created from the <SERVER_ROOT> of the Servlet/JSP container.

Table 1.2: The Web Application Directory Structure

Directory

Contains

/wroxapp

This is the root directory of the Web application. All JSP and HTML files are stored here.

/wroxapp/WEB-INF

This directory contains all resources related to the application that are not in the document root of the application. This is where your Web application deployment descriptor is located. You should note that the WEB-INF directory is not part of the public document. No files contained in this directory can be served directly to a client.

/ wroxapp/WEB-INF/classes

This directory is where servlet and utility classes are located.

/ wroxapp/WEB-INF/lib

This directory contains Java Archive (JAR) files that the Web application is dependent on.

If you're using Tomcat as your container, the default root directory is <CATALINA_HOME>/webapps/. Figure 1.2 shows the wroxapp as it would be hosted by a Tomcat container.

click to expand
Figure 1.2: The wroxapp Web application hosted by Tomcat.

start sidebar

Web applications allow compiled classes to be stored in both the /WEB-INF/ classes and /WEB-INF/lib directories. Of these two directories, the class loader will load classes from the /classes directory first, followed by the JARs in the /lib directory. If you have duplicate classes in both the /classes and /lib directories, the classes in the /classes directory will take precedence.

end sidebar

The Web Application Deployment Descriptor

The backbone of all Web applications is its deployment descriptor. The Web Application deployment descriptor is an XML file named web.xml that is located in the /<SERVER_ROOT>/application-name/WEB-INF/ directory. The web.xml file describes all of the components in the Web application. If we use the previous Web application name, wroxapp, then the web.xml file would be located in the /<SERVER_ROOT>/wroxapp /WEB-INF/ directory. The information that can be described in the deployment descriptor includes the following elements:

  • ServletContext init parameters

  • Localized content

  • Session configuration

  • Servlet/JSP definitions

  • Servlet/JSP mappings

  • Tag library references

  • MIME type mappings

  • Welcome file list

  • Error pages

  • Security information

This code snippet contains a sample deployment descriptor that defines a single servlet. We examine the web.xml file in much more detail as this text progresses.

 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC   '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'   'http://java.sun.com/dtd/web-app_2_3.dtd'> <servlet>   <servlet-name>SimpleServlet</servlet-name>   <servlet-class>com.wrox.SimpleServlet</servlet-class> </servlet> </web-app> 

Packaging a Web Application

The standard packaging format for a Web application is a Web Archive file (WAR). A WAR file is simply a JAR file with the extension .war as opposed to .jar. You can create a WAR file by using jar, Java's archiving tool. To create a WAR file, you simply need to change to the root directory of your Web application and type the following command:

 jar cvf wroxapp.war . 

This command produces an archive file named wroxapp.war that contains the entire wroxapp Web application. You can deploy your Web application by simply distributing this file.



 < Day Day Up > 



Professional Jakarta Struts
Professional Jakarta Struts (Programmer to Programmer)
ISBN: 0764544373
EAN: 2147483647
Year: 2003
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net