首页 > 代码库 > Servlet(四)Servlet映射URL

Servlet(四)Servlet映射URL

1、Servlet映射可以多层多样

   例子:

     

    <servlet>
        <servlet-name>HServlet</servlet-name>
        <servlet-class>com.hunhun.HServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HServlet</servlet-name>
        <url-pattern>/HServlet</url-pattern>
         <!-- <url-pattern>/abc.html</url-pattern> -->
         <!--  <url-pattern>/HServlet/abc</url-pattern> -->
    </servlet-mapping>
2、Servlet可以被映射到多个URL上

    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.hunhun.FirstServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/FirstServlet</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/GServlet</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/HServlet</url-pattern>
    </servlet-mapping>
可通过浏览器url:http://localhost:8080/myweb/FirstServlet|GServlet|HServlet访问FirstServlet
3、Servlet可使用通配符进行映射(只可以有两种格式)

 (1)使用  *.扩展名

    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.hunhun.FirstServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
可通过浏览器url:http://localhost:8080/myweb/abc.do访问FirstServlet

(2)使用 / 开始 /* 结束

   

    <servlet>
        <servlet-name>FirstServlet</servlet-name>
        <servlet-class>com.hunhun.FirstServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FirstServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

多个映射通配原则

(1)相似度高的优先匹配

 (2)*.扩展名的匹配方式优先级最低

Servlet(四)Servlet映射URL