HttpServletRequest getContextPath(),getRequestURI() ,getServletPath() ,getPathInfo()区别

getContextPath(),getRequestURI() ,getServletPath() ,getPathInfo()区别

getContextPath(): 获取当前Web应用的上下文路径(Context Path)。上下文路径是位于Web服务器域名资源路径之间的部分,以正斜杠 / 开始,但不以 / 结束。如果应用程序部署在服务器的根目录下(即默认上下文),则返回空字符串 "" 如果一个Web应用部署在 http://example.com/myapp 下,访问某个Servlet时,getContextPath() 方法将返回 "/myapp"

request.getRequestURI(): 包含了上下文路径(context path)和servlet路径,不包括查询参数(即URL中“?”之后的部分) 对于URL http://example.com/myApp/service/doSomething?param=value,如果应用上下文路径为 /myApp,则 getRequestURI() 将返回 /myApp/service/doSomething

request.getServletPath(): 不包括上下文路径(context path)和servlet路径,不包括查询参数(即URL中“?”之后的部分) 对于URL http://example.com/myApp/service/doSomething?param=value,如果应用上下文路径为 /myApp,则 getRequestURI() 将返回 /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映射之后剩余的具体路径信息。

你可能感兴趣的:(网络编程,java)