SLIDE 16 Understand Servlet Deployment! Deploying a Servlet is relatively simple but very important. Pay attention in the preceding example because for brevity further examples do not include the verbose deployment description. A single sentence such as “Deploy Servlet x to the URL mapping y” . is used to mean the same thing. Only when it is excep- tionally important to the example is the full deployment descriptor provided. Web Application Deployment Descriptor Structure Each and every Servlet needs to be deployed before it is available for a client to
- use. The HelloWorld Servlet example introduced the Web Application
Deployment Descriptor elements that do this, but before you go on deploying more Servlets, there is some more information to be aware of. The schema for
web.xml defines which elements can be used and in what order they must appear.
In the previous example this is the reason that both the servlet and servlet-
mapping elements appeared before the welcome-file-list element. This is also
the reason that the servlet element was required to appear before the servlet-
mapping element18.
From the preceding three elements it might seem arrangement is of alpha- betical precedence, but this is not the case. The arrangement of elements must match the given listing with the Web Application Deployment Descriptor schema. This rather long title should sound familiar—it is the same XML schema that defines what can appear in web.xml. The current complete schema can be found in the Servlet 2.4 specification. The element ordering is defined by the root web-inf element and is, in ascending order, as follows: icon, display-name, description,
distributable, context-param, filter, filter-mapping, listener, servlet, servlet-mapping, session-config, mime-mapping, welcome-file-list, error- page, jsp-config, resource-env-ref, message-destination-ref, resource-ref, security-constraint, login-config, security-role, env-entry, ejb-ref, ejb- local-ref., message-destination, and locale-encoding-mapping-list.
Understanding the order is not difficult, but it is a problem quite a few new Servlet developers ask about. It is well worth mentioning it now to avoid causing any confusion later. Keep in mind that this order also applies to multiple ele- ments of the same name. If two Servlets are deployed, both of the servlet ele- ments must be listed before any of the servlet-mapping elements. It does not
46
JAVA SERVLETS
- 18. Not all Servlet containers enforce the schema, however. Consult your container’s documentation
for more information.
falkner.ch2.qxd 8/29/03 1:00 PM Page 46