Web.xml the servlet (2/3) (turn) defined and mapped in the file

Source: Internet
Author: User

2. Map Servlets to URLs
The <servlet-mapping> element provides a URL pattern for a servlet instance. <servlet-mapping> elements must contain <servlet-name> elements and <url-pattern> elements.
The <servlet-name> element must be consistent with the <servlet-name> element defined in the Web.xml file somewhere <servlet> element. Because multiple instances of the same servlet in a container may be running, the container uses <servlet-name> to determine the mapping of a servlet.
The url/status/* of the following example is mapped to a servlet called GetStatus. If a servlet is defined by a container in http://example.roguewave.com/, then this element tells the container that the GetStatus servlet is going to access the requests that begins with the following
http://example.roguewave.com/examples/status.

<servlet-mapping>
<servlet-name>getStatus</servlet-name>
<url-pattern>/status/*</url-pattern>
</servlet-mapping>


A servlet can accept any number of URLs patterns requests. The following example shows two different pattern for 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>

Different pattern maps can be mapped to the same servlet, but the same pattern cannot be mapped to a different servlet. If a url-pattern in the same Web.xml file is mapped to a different servlet, the container cannot guarantee which servlet to invoke on a request.

2.1 URL Pattern Syntax:

Each character in a URL pattern must be in strict agreement with the URL path. By two exceptions, at the end of a pattern,/* matches any character after this point; *. the extension matches any file name that has this extension.
For example:
The matching pattern under/examples path of the server example.com <url-pattern>/status/*</url-pattern> can be matched as follows:
http://example.com/examples/status/synopsis
http://example.com/examples/status/complete?date=today
http://example.com/examples/status

does not match:
http://example.com/examples/server/status

The matching pattern under/examples path of the server example.com <url-pattern>*.map</url-pattern> can be matched as follows:
http://example.com/examples/us/oregon/portland.map
http://example.com/examples/us/washington/seattle.map
http://example.com/examples/paris.france.map

does not match
http://example.com/examples/us/oregon/portland.map
Uppercase name extension
http://example.com/examples/interface/description/mail.mapi
The extension is MAPI rather than map

Different filter in the same Web.xml file often uses the same url-pattern. In this case, each filter that matches the request will process the request.
Conversely, no two servlet maps to the same url-pattern in the same Web application, and if a url-pattern in the same Web.xml file maps to a different servlet, the container cannot guarantee a request which servlet to invoke. However, the two servlet can be partially consistent with url-pattern (that is,/* or *.extention), so which servlet the container invokes is determined by the matching process.

2.2 Servlet Matching Process
A request may match more than one servlet mapping. The container uses the straightforward matching procedure to determine which servlet best matches. The matching process has four simple rules:
1〉 strict matching takes precedence over matching with *;
2〉 the longest pattern takes precedence over other pattern;
3〉 path matching takes precedence over file type matching;
4〉pattern <url-pattern>/</url-pattern> always matches request with no other pattern matching.

For example:
The Web.xml file may map the home page of the online catalog to a pattern and map the query page of the online catalog to another pattern:

<servlet-mapping>
<servlet-name>catalogBrowse</servlet-name>
<url-pattern>/Catalog/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>catalogSearch</servlet-name>
<url-pattern>/Catalog/search/*</url-pattern>
</servlet-mapping>

The following figure describes the matching process: because the longest pattern takes precedence over other mode, URLs containing/catalog/search/are always mapped to CatalogSearch instead of Catalogbrowse.

2.3 Default servlet:
A request containing a pattern match of <url-pattern>/</url-patttern> with no other pattern matching. This is called the default mapping. The servlet that maps to the default mapping is called the default servlet.
The default mapping is generally to the first page of an application. Explicitly providing a default mapping ensures that the incorrect URL requests is mapped to a return that can be processed by the application instead of giving a return error.
The <servlet-mapping> element below maps the default mapping to the Welcome servlet instance
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Web.xml that contains this element sends a request to the welcome servlet that any other mapping cannot handle.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.