目录标题
request.getContextPath(): 获取当前Web应用的上下文路径(Context Path)
1・上下文路径是位于Web服务器域名和资源路径之间的部分,以正斜杠 / 开始,但不以 / 结束。
2・如果应用程序部署在服务器的根目录下(即默认上下文),则返回空字符串 “”。
3・例如:如果一个Web应用部署在 http://example.com/myapp
下,getContextPath()
方法将返回 “/myapp
”。
request.getRequestURI():上下文路径+servlet路径
对于URL http://example.com/myApp/service/doSomething?param=value
,如果应用上下文路径为 /myApp
,则 getRequestURI()
将返回 /myApp/service/doSomething
。
request.getServletPath():servlet路径
对于URL http://example.com/myApp/service/doSomething?param=value
,如果应用上下文路径为 /myApp
,则 getServletPath()
将返回 /service/doSomething
。
request.getPathInfo() :获取客户端请求URL中除Servlet映射路径之外的额外路径信息
如配置 @WebServlet("/api/*")
后,对于URL http://example.com/myApp/api/v1/users/42
,request.getServletPath()
可能会返回 /api
,因为这是Servlet映射的基础路径。 request.getPathInfo()
将返回 /v1/users/42
,这是匹配Servlet映射之后剩余的具体路径信息。