站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题Servlet/JSP 容器开发手记3 - ServletMapping的处理

Servlet/JSP 容器开发手记3 - ServletMapping的处理

摘要: 开发手记 Servlet JSP ServletMapping

注意: 下述所说的 URL 都未包含 ContextPath

Servlet 映射的 URL 规范

1. 以 / 开始并以 /* 结束的表示路径映射, 即 /Test/* 表示请求路径以 /Test/ 开始的 URL 映射到该 Servlet 处理
2. 以 *. 开始的表示扩展名映射, 即 *.abc 表示所有以扩展名为 abc 的请求映射到该 Servlet 处理
3. 以 / 开始的表示完全映射, 即 /Test 表示请求 URL 为 /Test 映射到该 Servlet 处理
4. URL Pattern 为 / 表示映射成默认的 Servlet, 当其他 URL 映射未匹配时, 使用默认的 Servlet


路径匹配顺序

1. 先匹配完全映射的 URL
2. 再次匹配路径映射的 URL, 并且最长的路径的匹配优先
3. 再次匹配扩展名映射
4. 最后使用默认的 Servlet

以下为示例

Servlet 映射

path pattern servlet

/foo/bar/* servlet1
/baz/* servlet2
/catalog servlet3
*.bop servlet4

incoming path servlet handling request

/foo/bar/index.html servlet1
/foo/bar/index.bop servlet1
/baz servlet2
/baz/index.html servlet2
/catalog servlet3
/catalog/index.html “default” servlet
/catalog/racecar.bop servlet4
/index.bop servlet4

各种匹配模式下的 ServletPath 和 PathInfo

1. 完全匹配 ServletPath = 匹配模式 PathInfo = null
2. 扩展名匹配 ServletPath = 请求的URL PathInfo = null
3. 路径匹配 ServletPath = 匹配模式剪去 "/*" PathInfo = 请求URL 剪去 ServletPath
4. 默认 Servlet 匹配 ServletPath = 请求的URL PathInfo = null

如果同样的 URL 被映射到多个 Servlet 时, Serlvet 规范未做说明, Tomcat 使用最后一个映射的 Servlet
如果覆盖了容器内设的 Servlet 映射, 使用新的映射



↑返回目录
前一篇: Servlet/JSP 容器开发手记4-Request Parameter
后一篇: 巧用文件保存对象来提高JSP的性能