Jsp&servelet Learning Notes-Create multiple mapping for a servlet

Source: Internet
Author: User

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 >

 

Related Article

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.