在web描述符文件web.xml文件中,可以为同一个servlet定义多个servlet-mapping;也可以在同一个servlet-mapping中,定义多个url-pattern。也就是说,可以把多个地址(相对于上下文路径)映射到同一个servlet处理。
- XML Schema 《web-common_6_0.xsd》中对servlet-mapping的定义:
csharp
<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>
样例:
csharp
<servlet>
<description>JAX-WS endpoint - restful</description>
<display-name>restful</display-name>
<servlet-name>restful-addnumbers</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restful-addnumbers</servlet-name>
<url-pattern>/addnumbers/*</url-pattern>
<url-pattern>/addnumbers2/*</url-pattern>
</servlet-mapping>