Web Deployment Descriptors
Deployment descriptors are text-based XML files that describe how a J2EE component is deployed into an application server. Deployment descriptors allow additional attributes such as security settings and initialization properties, which are not represented in Java code, to be associated with the component. J2EE defines several deployment descriptors; however, there are three major ones:
In this chapter, we have focused solely on servlets and as such the only descriptor we will describe at this point is web.xml. Later we will discuss the application.xml and ejb-jar.xml files.
The web.xml deployment descriptor is included in the Web WAR file and describes to the Web container how the items in the file are to be deployed and used in the Web container. A simple web.xml, which sets up a servlet mapping for the HelloServlet created previously, follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http:/ /java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Hello Web Module</display-name> <servlet> <servlet-name>SimpleHello</servlet-name> <display-name>SimpleHello</display-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SimpleHello</servlet-name> <url-pattern>sayHello</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Notice that there are essentially three key elements in the web.xml file: (1) The servlet element associates a name with a Java class name for the servlet. (2) The servlet-mapping element associates a URL pattern with the named servlet. (3) The welcome-file-list element gives a list of files that can be used as the home page for the WAR file that contains this web.xml file. By convention, the welcome files are returned by default if no specific file is requested.
Once a servlet is named in a web.xml file, initialization parameters can be specified and the servlet can be marked for initial startup. Additionally, other elements, which deal with EJB references, security, and environment variables can be specified in the web.xml file.