Web applications consist of the following components: JSPs, TagLibs, and servlets. You describe these components and their relationship with a metadata deployment filed named web.xml. The web.xml file is a deployment descriptor defined by the Servlet Specification. The deployment descriptor is stored in the root of the WEB-INF directory. The Web application deployment descriptor holds the following information for the Web application container:
ServletContext init parameters
Session configuration
Servlet/JSP definitions
Servlet/JSP URI mappings
Mime type mappings
Error pages
Security
JNDI environment
Referenced EJBs
Maps resources, such as JDBC connections, URL factory, JMS resources, and mail resources
The Web application is usually contained in a WAR file. A WAR file is a single archive file with the .war file extension. Like a JAR file, a WAR file uses the ZIP file format. Unlike a JAR file, a WAR file cannot be placed on the classpath. The WAR file contains all the components of a Web application, including support libraries and HTML files. The WAR file holds the HTML and JSP files in the root of the WAR file, and it holds the servlets and related classes in the WEB-INF/classes directory. Any supporting libraries (JAR files) the JSP or servlets need are held in the WEB-INF/lib directory. A WAR file can hold all the files a Web application needs for deployment.
A directory structure for a Web application may look something like this:
Web Application Archive file Root index.html HelloWorld.jsp \---WEB-INF web.xml +---classes \---xptoolkit \---web HelloWorldServlet.class \---lib greetmodel.jar
This example has index.html and HelloWorld.jsp in the root directory. It also has the servlet xptoolkit.web.HelloWorldServlet in the /WEB-INF/classes directory. In addition, the support library greetmodel.jar is in the /WEB-INF/lib directory. The greetmodel.jar file has JavaBeans and classes that are needed by HelloWorld.jsp and xptoolkit.web.HelloWorldServlet. Note that this example is based on a sample we will use in chapters 5 and 6.
As we stated earlier, the web.xml file sets environment settings for the Web application. An example deployment descriptor for a Web application from previous WAR file may look like this:
<web-app> <error-page> <error-code>404</error-code> <location>/HelloWorldServlet</location> </error-page> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>xptoolkit.web.HelloWorldServlet</servlet-class> <init-param> <param-name>Greeting.class</param-name> <param-value>xptoolkit.model.HelloWorldBean</param-value> </init-param> </servlet> <servlet> <servlet-name>HelloWorldJSP</servlet-name> <jsp-file>HelloWorld.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>HelloWorldJSP</servlet-name> <url-pattern>/HelloWorldJSP</url-pattern> </servlet-mapping> </web-app>
This deployment descriptor creates two servlet definitions: one for HelloWorld.jsp (a JSP compiles to a servlet before use) and one for xptoolkit.web.HelloWorldServlet. The deployment descriptor then maps a few URI mappings for the servlets that were defined. A servlet mapping maps a servlet to a URI.
This was just an introduction to the Web application and WAR files. For a detailed description, refer to the Java Servlet Specification (http://java.sun.com/j2ee/). The parts of the deployment descriptor that we used here are explained as we build the sample applications deployed in Chapter 6, Building J2EE Applications with Ant.