zoukankan      html  css  js  c++  java
  • 关于Servlet和Filter的url-pattern匹配规则一二三

    参考链接

    二者的规则是一样的,先放参考链接

    https://www.itread01.com/content/1550443512.html

    https://my.oschina.net/u/2474629/blog/693115

    https://coderanch.com/t/474573/certification/ContextPath-ServletPath

    https://docs.oracle.com/cd/E19879-01/819-3669/6n5sg7b0b/index.html

    https://javaee.github.io/servlet-spec/downloads/servlet-4.0/servlet-4_0_FINAL.pdf[查看Mapping Requests to Servlets一节]

    url-pattern形式

    主要分为完全匹配,路径匹配,扩展名匹配这三个:

    ■A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

    ■A string beginning with a ‘*.’ prefix is used as an extension mapping.

    ■The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<context-root>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).

    ■A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

    ■All other strings are used for exact matches only

    url-pattern匹配规则

    匹配规则按照完全>最长路径>扩展名>默认servlet或者路径资源

    1. The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.

    2. The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the ’/’ character as a path separator. The longest match determines the servlet selected.

    3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last ’.’ character.

    4. If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a "default" servlet is defined for the application, it will be used. Many containers provide an implicit default servlet for serving content.

    其中Filter的url匹配还要注意Dispatcher方式,分为REQUEST, FORWARD, INCLUDE, ERROR四种类别,默认是REQUEST

    知识活用

    <url-pattern>/</url-pattern>  会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
    <url-pattern>/*</url-pattern> 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)

    这两句话,相信关注相关问题的百度都会经常看到,按照匹配规则,/*路径匹配了所有的url,/是默认servlet,只能匹配部分。需要注意的是一般servlet容器都有默认的jsp扩展名匹配的servlet,所以才有上面的后半句话

    小结

    其实这块内容容易搞混的原因主要是因为对于servlet规范这块不是很清楚,参考https://javaee.github.io/servlet-spec/downloads/servlet-4.0/servlet-4_0_FINAL.pdf这个说明就好,没必要全部看。

    理解了之后,对于request的PathInfo,ContextPath,Servletpath这些其实也能很好理解了,参考https://coderanch.com/t/474573/certification/ContextPath-ServletPath

  • 相关阅读:
    谷歌三架马车之 The Google File System 中文版
    数据集市 Data Mart
    VMware虚拟机ubuntu下安装VMware Tools步骤
    NOIP普及组 海港 题解
    HXD的DS
    离散化
    哈希表
    状态压缩DP 初探
    《信息学奥赛一本通》大盗阿福 题解
    NOIP 加工零件 题解
  • 原文地址:https://www.cnblogs.com/marshwinter/p/13949082.html
Copyright © 2011-2022 走看看