JaVa
   

Web apps and the WAR File

Web apps 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 app deployment descriptor holds the following information for the Web app container:

The Web app 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 app, 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 app needs for deployment. A directory structure for a Web app may look something like this:

Web app 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 s 5 and 6. As we stated earlier, the web.xml file sets environment settings for the Web app. An example deployment descriptor for a Web app 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 app and WAR files. For a detailed description, refer to the Java Servlet Specification (http://java.oracle.com/j2ee/). The parts of the deployment descriptor that we used here are explained as we build the sample apps deployed in , “Building J2EE apps with Ant.”


JaVa
   
Comments