2. Serving Servlets to URLs
The <servlet-mapping> element provides a URL pattern for a servlet instance. The <servlet-mapping> element must contain the <servlet-Name> element and <URL-pattern> element.
The <servlet-Name> element must be consistent with the <servlet-Name> element defined by the <servlet> element somewhere in the web. xml file. Since multiple instances may be running in the same servlet of a container, the container uses <servlet-Name> to determine the servlet ing of a servlet.
The URL/status/* in the following example is mapped to a servlet called getstatus. If a servlet is defined by a container in http: // example.roguewave.com/, this element tells the container that getstatus servlet will access requests starting
Http: // example.roguewave.com/examples/status.
<Servlet-mapping>
<Servlet-Name> getstatus </servlet-Name>
<URL-pattern>/status/* </url-pattern>
</Servlet-mapping>
A servlet can accept requests of any number of URL patterns. 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 can be mapped to the same servlet, but the same pattern cannot be mapped to different servlets. If a URL-pattern in the same web. xml file is mapped to different Servlets, the container cannot guarantee which servlet is called for a request.
2.1 URL pattern Syntax:
Each character in a URL pattern must be exactly the same as that in the URL path. With two exceptions, at the end of a pattern,/* matches any character after this vertex; *. The extension matches any file name with this extension.
For example:
Server example.com path/matching mode under examples <URL-pattern>/status/* </url-pattern> can match as follows:
Http: // example.com/examples/status/synopsis
Http: // example.com/examples/status/complete? Date = today
Http: // example.com/examples/status
Mismatch:
Http: // example.com/examples/server/status
The server example.com path/matching mode under examples <URL-pattern> *. Map </url-pattern> can match 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
Mismatch
Http: // example.com/examples/us/oregon/portland.map
// Uppercase Extension
Http: // example.com/examples/interface/description/mail.mapi
// The extension is mapi rather than Map
Different filters in the same web. xml file often use the same URL-pattern. In this case, each filter that matches the request will process the request.
On the contrary, no two servlets in the same web application map to the same URL-pattern. when a URL-pattern in an XML file is mapped to different Servlets, the container cannot guarantee which servlet is called for a request. However, the two servlets are available with the same URL-pattern (I .e./* or *. extention). In this way, the matching process determines which servlet is called by the container.
2.2 servlet matching process
One request may match more than one servlet ing. The matching process of containers using straightforward determines which servlet is the most matched. The matching process has four simple rules:
1> strict matching takes precedence over matching;
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 requests without other pattern matches.
For example:
The Web. xml file may map the home page of the online directory to a pattern, and map the query page of the online directory 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 matching process is described: Because the longest pattern takes precedence over other pattern, URLs containing/CATALOG/search/are always mapped to catalogsearch, rather than catalogbrowse.
2.3 default servlet:
The pattern that contains <URL-pattern>/</url-patttern> matches no other request that matches pattern. This is called default ing. The servlet mapped to the default servlet ing is called the default servlet.
The default ing is generally the first page of an application. Explicitly provides a default ing to ensure that the incorrect URL requests is mapped to the return that can be processed by the application rather than a return error.
The following <servlet-mapping> elements map to the welcome servlet instance by default.
<Servlet-mapping>
<Servlet-Name> welcome </servlet-Name>
<URL-pattern>/</url-pattern>
</Servlet-mapping>
The Web. XML that contains this element sends any requests that cannot be processed by other mappings to the welcome servlet.