Request的getRequestURI()和getPathInfo()

在servlet处理请求的路径时request里有几个常用的函数:

getRequestURL()
getRequestURI()
getContextPath()
getServletPath()
getPathInfo()

这里的前三个函数的返回值基本是可以预期的,与servlet映射的URL基本没关系,而后两个返回的值就是与servlet映射的URL相关的了。(对于URL和URI的关系,请看这里

先看一下前三个:

访问 : http://localhost:8080/myapp/test/first.html
URL : http://localhost:8080/myapp/test/first.html
URI : /myapp/test/first.html
ContextPath : /myapp

StringBuffer getRequestURL()
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:

First line of HTTP request Returned Value
POST /some/path.html HTTP/1.1 /some/path.html
GET http://foo.bar/a.html HTTP/1.0 /a.html
HEAD /xyz?a=b HTTP/1.1 /xyz

String getContextPath()
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

关于PathInfo和ServletPath

情况1(path mapping):
@WebServlet("/test1/*")
Request : http://localhost:8080/myapp/test1/first.html
ServletPath : /test1
PathInfo : /first.html

情况2(exact match):
@WebServlet("/test2/t2")
Request : http://localhost:8080/myapp/test2/t2
ServletPath : /test2/t2
PathInfo : null

情况3(extension mapping):
@WebServlet("*.do")
Request : http://localhost:8080/myapp/test3/t3.do
ServletPath : /test3/t3.do
PathInfo : null

情况4(default match):
@WebServlet("/")
Request : http://localhost:8080/myapp/
ServletPath : /
PathInfo : null

String getPathInfo()
Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.
This method returns null if there was no extra path information.

String getServletPath()
Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.
This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

扩展阅读:
关于servlet mapping

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • =========================================================...
    lavor阅读 8,842评论 0 5
  • 已经同步到gitbook,想阅读的请转到gitbook: Django 1.10 中文文档 URL dispatc...
    leyu阅读 14,819评论 0 16
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 7,951评论 0 0
  • 最后你还是离开我了。 我们那么小心翼翼,不要压力,不要诺言,不要认真的努力相处。 最后还是会分开。 还是不能忍受,...
    赵铖zxy阅读 1,748评论 0 1