Developing Web Applications With Ant by Richard Hightower - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

The Deployment Descriptor for the Hello World Web Application

In order to configure the JSPs and servlets, we need a deployment descriptor. The following code defines a simple deployment descriptor that assigns names and mappings to the JSPs and servlet. Please note that the deployment descriptor goes in the web.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<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>@Greeting.class@</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>

The deployment descriptor defines two servlet elements: one for HelloWorldServlet and one for HelloWorldJSP. If you are wondering why there is a servlet element for HelloWorldJSP, remember that HelloWorld.jsp is compiled to a servlet before it is used for the first time. The HelloWorldServlet servlet element maps to the servlet (<servlet-class>xptoolkit.web.HelloWorldServlet</servlet-class>). The HelloWorldJSP element maps to the JSP file HelloWorld.jsp (<jsp-file>HelloWorld.jsp</jsp-file>). Then, the servlet mapping elements map the servlet element to specific URL patterns.

Thus HelloWorldServlet maps to /HelloWorldServlet (<url-pattern>/HelloWorldServlet</url-pattern>); this is relative to the Web application location from the root of the server. And the HelloWorldJSP servlet element is mapped to the /HelloWorldJSP URL pattern (<url-pattern>/HelloWorldJSP</url-pattern>).

The build file must deploy the descriptor to a place where the application server can find it. It does this by packaging the HTML files, JSP files, Java servlet, and deployment descriptor in a WAR file. The next section describes the build file for this project.