Java Reference
In-Depth Information
Deploying a Web Application
In this chapter, we've developed some servlets, some JSP pages, a JavaBeans com-
ponent, and a tag library and its TLD. In order to tie all these examples together
into a single “web application,” you need to create the WEB-INF/web.xml file that
describes the web application and place all the necessary files in all the appropri-
ate places. An optional, but useful, next step is to archive all these files into a sin-
gle WAR file that can be easily distributed and deployed.
Configuring Web Applications with web.xml
The web.xml file is listed in Example 18-11. It is an XML file that contains tags that
provide various types of information about the web application. Version 2.2 of the
Servlet specification contains complete details about the format and content of this
file. The example demonstrates the most commonly used tags but omits a number
of less-frequently used tags. The most important tags in a web.xml file are the
<servlet> tags. A <servlet> tag specifies a mapping from a servlet name to a
servlet class and defines the initialization parameters for the servlet. (Recall that
servlets typically read their initialization parameters from the init() method.) To
make this web application work on your system, you have to change the values of
some of these initialization parameters. In particular, the countfile parameter of
the counter servlet should be edited, and all the JDBC initialization parameters of
the query servlet should be set to values appropriate for the database server you
are using.
Note that it is perfectly legal to define multiple names for the same servlet imple-
mentation class. Each <servlet> tag defines a separate instance of the servlet
class, and each instance can have a different set of initialization parameters. For
example, if you wanted to use the Query servlet to talk to two different database
servers, you could use two separate <servlet> tags that give the names queryDB1
and queryDB2 to two different instances of the Query class.
After all the <servlet> tags in the web.xml file comes a <servlet-mapping> tag. *
This tag is used to define a mapping from a URL prefix or suffix to a particular
named servlet instance. When the web server receives a request for any URL that
matches the specified pattern, it invokes the named servlet. In the example, I've
used a <servlet-mapping> tag to map any URLs ending in .count to the counter
servlet.
The <session-config> tag specifies session management information. In this
example, it indicates that user sessions time out after 15 minutes without a request
from the user. This tag is followed by a <taglib> tag that defines a mapping from
the unique URI of the custom tag library to the local location of the TLD file for
that library.
* The XML DTD for the web.xml file defines the required order of the tags. You are not free to rearrange
them but must keep them in the order shown in this example.
Search WWH ::




Custom Search