在web描述符文件web.xml文件中,可以为同一个servlet定义多个servlet-mapping;也可以在同一个servlet-mapping中,定义多个url-pattern。也就是说,可以把多个地址(相对于上下文路径)映射到同一个servlet处理。
- XML Schema 《web-common_6_0.xsd》中对servlet-mapping的定义:
<xsd:element name="servlet-mapping"
type="jakartaee:servlet-mappingType"/>
...
<xsd:complexType name="servlet-mappingType">
<xsd:annotation>
<xsd:documentation>
The servlet-mappingType defines a mapping between a
servlet and a url pattern.
Used in: web-app
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="servlet-name"
type="jakartaee:servlet-nameType"/>
<xsd:element name="url-pattern"
type="jakartaee:url-patternType"
minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="id"
type="xsd:ID"/>
</xsd:complexType>
- https://tomcat.apache.org/tomcat-10.1-doc/appdev/web.xml.txt
样例:
<servlet>
<servlet-name>stock</servlet-name>
<servlet-class>async.AsyncStockServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>stock</servlet-name>
<url-pattern>/async/stockticker</url-pattern>
</servlet-mapping>