Registering a Web application (designating the location of the top-level directory and specifying a URL prefix) is a completely server-specific process. For information on the registration process, see Section 4.1 (Registering Web Applications). Here, I’ll just show the settings necessary for Tomcat.
Listing 6.1 presents the relevant part of the Tomcat server.xml file. Recall that, if you use the default Web application settings, it is only necessary to edit server.xml in Tomcat 3. In Tomcat 4, just drop the boats directory into install_dir/webapps.
<?xml version="1.0" encoding="ISO-8859-1"?>
<Server>
...
<Context path="/boats" docBase="boats" />
</Server> |
Listing 6.2 shows the complete web.xml file. Recall from Section 5.2 (The Order of Elements within the Deployment Descriptor) that it is important to arrange the web.xml elements within web-app in the proper order. Also, because the boats application does not use filters, life-cycle event handlers, or other new features, it uses the servlet 2.2 DTD to maximize portability.
This version of web.xml specifies the following behaviors:
The ShowItem servlet can be accessed with the URL http://host/boats/DisplayItem instead of the default URL of http://host/boats/servlet/moreservlets.ShowItem.
Similarly, the ShowPurchases servlet can be accessed with the URL http://host/boats/DisplayPurchases instead of http://host/boats/servlet/moreservlets.ShowPurchases.
The file index.jsp (if it exists) should be used as the default filename for URLs that name a directory but not a file. If index.jsp is not found, index.html should be used. If neither is found, the result is server specific.
For more information on using the web.xml file, see Chapter 5 (Controlling Web Application Behavior with web.xml).
<?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> <!-- Register names for the ShowItem and ShowPurchases servlets. These names will be used with servlet-mapping to set custom URLs. --> <servlet> <servlet-name>ShowItem</servlet-name> <servlet-class>moreservlets.ShowItem</servlet-class> </servlet> <servlet> <servlet-name>ShowPurchases</servlet-name> <servlet-class>moreservlets.ShowPurchases</servlet-class> </servlet> <!-- Set the URL http://host/webAppName/DisplayPurchases to invoke the servlet that would otherwise be available with the URL http://host/webAppName/servlet/moreservlets.ShowPurchases --> <servlet-mapping> <servlet-name>ShowPurchases</servlet-name> <url-pattern>/DisplayPurchases</url-pattern> </servlet-mapping> <!-- Set the URL http://host/webAppName/DisplayItem to invoke the servlet that would otherwise be available with the URL http://host/webAppName/servlet/moreservlets.ShowItem --> <servlet-mapping> <servlet-name>ShowItem</servlet-name> <url-pattern>/DisplayItem</url-pattern> </servlet-mapping> <!-- If URL gives a directory but no filename, try index.jsp first and index.html second. If neither is found, the result is server specific (e.g., a directory listing). --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> |
3.135.249.220