Servlet(Url)

在web.xml文件中,以下语法用于定义映射: 

    1.  以“/”字符开头,并以“/*”结尾的字符串用于路径映射。

    2.  以“*.”开头的字符串被用于扩展名映射。

    3.  空字符串("")是用于指定精确映射应用程序 context root 的 URL 模式,比如从 
    http://host:port/<contextroot>/ 来的请求。在这种情况下路径信息是“/”,servlet 
    路径和 context 路径是一个空的字符串("")。

    4.  字符串中仅有“/”字符时,表示应用程序“默认”的 servlet。在这种情况下 servlet 
    路径是请求 URI 去掉 context 路径,且路径信息为 null。

    5.  其他的字符串仅用于精确匹配。

 

匹配规则和顺序如下:    

 1. 精确路径匹配(Map exact URL)    例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,
这个时候,如果我访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被
servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。 

    2. 最长路径匹配(Map wildcard paths)    例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 

    3. 扩展匹配(Map extensions)    如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA
 的url-pattern:*.action 

    4. (Map to the default servlet)    如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet,则容器会将请求丢给default servlet

 

Note

url-pattern must start with '/' or '*.'

 

1.<url-pattern>/*</url-pattern>

        The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer 

such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet.

 This is thus a bad URL pattern for servlets. Usually, you'd like to use /* on a Filter only

2.<url-pattern>/</url-pattern>

        The / doesn't override any other servlet. It only replaces the servletcontainer's builtin default servlet 

for all requests which doesn't match any other registered servlet. This is normally only invoked on static

 resources (CSS/JS/image/etc) and directory listings'.

3.<url-pattern></url-pattern>

        Then there's also the empty string URL pattern . This will be invoked when the context root is requested. 

This is different from the <welcome-file> approach.

4.Front Controller

        In case you actually intend to have a front controller servlet, then you'd best map it on a more specific 

URL pattern like *.html, *.do, /pages/*, /app/*, etc. You can hide away the front controller URL pattern 

and cover static resources on a common URL pattern like /resources/*, /static/*, etc with help of a 

servlet filter.

 

对于filter,不会像servlet那样只匹配一个servlet,因为filter的集合是一个链,所以只会有处理的顺序不同,而不会出现只选择一个filter。Filter的处理顺序和filter-mapping在web.xml中定义的顺序相同。 

 

 

转载于:https://my.oschina.net/igooglezm/blog/658620

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值