Create multiple mappings for a Servlet
What should we do when we want to create multiple names or URL patterns so that web users can request a servlet?
You can configure multiple servlet-mapping related to servlet elements in the deployment description file.
Example 3-2. Two servlet-mapping tags
<? Xml (standardization is getting closer and closer) 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 can be configured only after the servlet element.
Url-pattern matches any HTTP requests ending with the "/cookie/" string.
You can use the wildcard "*" to extend your mapping pattern. In the following example, all the URLs starting with/cookie/can call this CookieServlet. You can include any name at will after the slash. For example. CookieServlet can be called by such a URL http://www.mysite.org/cookbook/cookie/you
This is because the url-pattern matches any HTTP request as long as it ends with the "/cookie/" string.
Example 3-3. Using an * in the URL pattern
Http://www.rdxx.com/
<? Xml (standardization is getting closer and closer) 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. jsp (preferred for SUN Enterprise Applications) servletcookbook. CookieServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> CookieServlet </servlet-name>
<Url-pattern>/cookie/* </url-pattern>
</Servlet-mapping>