Request URI = context path + servlet path + path info
2015-06-16 23:17
387 查看
|-- Context Path --|-- Servlet Path -|--Path Info--| http://www.servername.com /mywebapp /helloServlet /hello |-------- Request URI ----------------------------| 1. Context paths and servlet paths start with a / but do not end with it. 2. HttpServletRequest provides three methods getContextPath(),getServletPath() and getPathInfo() to retrieve the context path, the servlet path, and the path info, respectively, associated with a request. Identifying the servlet path To match a request URI with a servlet, the servlet container follows a simple algorithm. Once it identifies the context path, if any, it evaluates the remaining part of the request URI with the servlet mappings specified in the deployment descriptor, in the following order. If it finds a match at any step, it does not take the next step.1 The container tries to match the request URI to a servlet mapping. If it finds a match, the complete request URI (except the context path) is the servlet path. In this case, the path info is null. 2 It tries to recursively match the longest path by stepping down the request URI path tree a directory at a time, using the / character as a path separator, and determining if there is a match with a servlet. If there is a match, the matching part of the request URI is the servlet path and the remaining part is the path info. 3 If the last node of the request URI contains an extension (.jsp, for example), the servlet container tries to match it to a servlet that handles requests for the specified extension. In this case, the complete request URI is the servlet path and the path info is null. 4 If the container is still unable to find a match, it will forward the request to the default servlet. If there is no default servlet, it will send an error message indicating the servlet was not found. 1. 精确路径匹配。例子:比如servletA 的url-pattern为 /test,servletB的url-pattern为 /* ,这个时候,如果访问的url为http://localhost/test ,这个时候容器就会先进行精确路径匹配,发现/test正好被servletA精确匹配,那么就去调用servletA,也不会去理会其他的servlet了。 2. 最长路径匹配。例子:servletA的url-pattern为/test/*,而servletB的url-pattern为/test/a/*,此时访问http://localhost/test/a时,容器会选择路径最长的servlet来匹配,也就是这里的servletB。 3. 扩展匹配,如果url最后一段包含扩展,容器将会根据扩展选择合适的servlet。例子:servletA的url-pattern:*.action 4. 如果前面三条规则都没有找到一个servlet,容器会根据url选择对应的请求资源。如果应用定义了一个default servlet 为什么定义”/*.action”这样一个看起来很正常的匹配会错?因为这个匹配即属于路径映射,也属于扩展映射,导致容器无法判断。
相关文章推荐
- IOS Exception2 this class is not key value coding-compliant for the key Click
- android源码之Builder
- poj 3061 Subsequence 【尺取法 or STL lower_bound】
- unique_ptr优于auto_ptr的原因
- CocoaUI 是一个强大的 iOS UI 框架
- sublime text3 中 CTags: Rebuild Tags执行之乱码解决方法
- Java优先级队列PriotyQueue
- STL之deque容器详解
- Android UI Dialog 使用
- 体验IBM Bluemix新一代云应用程序开发平台
- 【WinRT】获取 Uri 中的参数(QueryString)键值对
- java中的StringBuffer和StringBuilder
- UGUI 点击穿透问题
- [HDU 3415] Max Sum of Max-K-sub-sequence · 单调队列
- 自定义Loader 和 Handler传递参数给UI线程
- 当“逻辑”与“UE”冲突时
- bzoj 2482: [Spoj1557] Can you answer these queries II
- servlet跳转(RequestDispatcher)的获取
- iOS中UIView之间布局及跳转的几种方式
- HBuilder:一个不错的web前端IDE(代码编辑器)