Jsp & amp; Servelet learning notes-create multiple mappings for a Servlet

Source: Internet
Author: User

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>

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.