Servlet 2.3之前的版本使用DTD作为部署描述文件的定义,其web.xml的格式为如下所示:
<?xml version="1.0" encoding="IS0-8859-1"?> <!DOCTYPE web-app PUBLIC "-//sunMicrosystems,Inc.//DTD WebApplication 2.3f//EN" "http://java.sun.com/j2ee/dtds/web-app_2.3.dtd"> <web-app> ....... </web-app>
Servlet 2.4版首次使用XML Schema定义作为部署描述文件,这样Web容器更容易校验web.xml语法。同时XML Schema提供了更好的扩充性,其web.xml中的格式如下所示:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns=http://java.sun.com/xml/ns/j2ee xmlns:workflow=http://www.workflow.com xmins:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> ......... </web-app>
XML Schema Definition: tomcat6.0\lib\servlet-api.jar javax\servlet\resources\web-app_2_4.xsd
部分说明:
<xsd:annotation> <xsd:documentation> <![CDATA[ This is the XML Schema for the Servlet 2.4 deployment descriptor. The deployment descriptor must be named "WEB-INF/web.xml" in the web application's war file. All Servlet deployment descriptors must indicate the web application schema by using the J2EE namespace: http://java.sun.com/xml/ns/j2ee and by indicating the version of the schema by using the version element as shown below: <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="..." version="2.4"> ... </web-app> The instance documents may indicate the published version of the schema using the xsi:schemaLocation attribute for J2EE namespace with the following location: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ]]> </xsd:documentation> </xsd:annotation>
最新的Servlet 2.5版XML Schema web.xml中的格式:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5"> ... </web-app>