Create multiple mapping for a servlet
What to do when we want to create multiple name or URL patterns so that Web users can request a servlet.
Then the servlet-mapping associated with the servlet element in the deployment description file can be configured multiple.
Example 3-2. Two servlet-mapping tags
? XML version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app
Public "-//sun Microsystems, INC.//DTD Web application 2.3//en"
"Http://java.sun.com/dtd/web-application_2_3.dtd"
>
< Web-app >
< servlet >
< Servlet-name > Cookieservlet </servlet-name >
< Servlet-class > Com.parkerriver.cookbook.CookieServlet </servlet-class >
</servlet >
< servlet-mapping >
< Servlet-name > Cookieservlet </servlet-name >
< Url-pattern >/cookieservlet </url-pattern >
</servlet-mapping >
< servlet-mapping >
< Servlet-name > Cookieservlet </servlet-name >
< Url-pattern >/mycookie </url-pattern >
</servlet-mapping >
</Web-app >
Note: The servlet-mapping element must be configured after the servlet element.
Url-pattern matches any HTTP requests ending with the "/cookie/" string.
You can use the wildcard character "*" to extend your mapping pattern. This cookieservlet can be invoked in the following example, which starts with the/cookie/URL. At the back of the slash can optionally include any name. Give me a small example. Cookieservlet can be called by such a URL http://www.mysite.org/cookbook/cookie/you
That's because Url-pattern matches any HTTP request as long as it ends with a "/cookie/" string.
Example 3-3. Using the URL pattern
? xml version= "1.0" encoding= "iso-8859-1"?>
<! Doctype web-app
PUBLIC "-//sun microsystems, inc.//dtd web application 2.3//en "
" http:// Java.sun.com/dtd/web-application_2_3.dtd "
>
< servlet >
< servlet-name > Cookieservlet </servlet-name >
< servlet-class > Com.jspservletcookbook.CookieServlet </servlet-class >
</servlet >
< servlet-mapping >
< Servlet-name ; Cookieservlet </servlet-name >
< url-pattern >/cookie/* </url-pattern >
</S ervlet-mapping >