Mapping Servlets to URLs
The servlet-mapping element associates a URL pattern to a servlet instance. The servlet-mapping always contains a servlet-name element and a url-pattern element.
The servlet-name element must match a servlet-name defined in a servlet element elsewhere in the web.xml file. Since a servlet container may have multiple instances of the same servlet running, the container uses the servlet-name to associate a mapping with a servlet.
The
url-pattern element contains a pattern that the container uses to determine whether an incoming request should be forwarded to a given servlet. In simple terms, the
url-pattern determines the location of the servlet within the context. The
url-pattern element can also contain more sophisticated mappings, as described in
URL Patterns.For example, the web.xml fragment below maps the URL /status/* to a servlet named getStatus. If the servlet is defined in the examples context of a container at http://example.com/, then this element tells the container that the getStatus servlet should receive requests for URLs that start with http://example.com/examples/status.
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>
A servlet may receive requests from any number of URL patterns. The fragment below maps two different patterns to the same servlet instance:
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/briefStatus/*</url-pattern>
</servlet-mapping>
Although different patterns can map to the same servlet, the same pattern cannot map to two different servlets. If the same
url-pattern appears more than once within a
web.xml file, the HydraExpress Agent makes no guarantees about which servlet receives a request.
URL Patterns describes the
url-pattern element in detail.