getRequestURI()与getRequestURL()的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了getRequestURI()与getRequestURL()的区别相关的知识,希望对你有一定的参考价值。

引于: http://hi.baidu.com/cloudxpc

request.getRequestURI() 返回值类似:/xuejava/requestdemo.jsp

request.getRequestURL() 返回值类似:http://localhost:8080/xuejava/requestdemo.jsp

 

附:

request.getContextPath() = /hboys
request.getLocalAddr() = 127.0.0.1
request.getPathInfo() = null
request.getPathTranslated() = null
request.getRemoteAddr() = 127.0.0.1
request.getRequestURI() = /hboys/
request.getScheme() = http
request.getServerName() = 127.0.0.1
request.getServletPath() = /index.jsp
request.getClass() = class org.apache.catalina.connector.RequestFacade
request.getHeaderNames() = 
request.getLocale() = zh_CN
request.getLocales() = 
request.getParameterMap() = {}
request.getRequestURL() = http://127.0.0.1:8081/hboys/
request.getUserPrincipal() = null
request.getParameterNames() = 
request.getRealPath("newsPub") = D:\workspace\hboys\WebRoot\newsPub

 

假定你的web application 名称为news,你在浏览器中输入请求路径:

http://localhost:8080/news/main/list.jsp

则执行下面向行代码后打印出如下结果:

1、 System.out.println(request.getContextPath());

打印结果:/news
   2、System.out.println(request.getServletPath());

打印结果:/main/list.jsp
3、 System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp
4、 System.out.println(request.getRealPath("/"));

打印结果:F:\Tomcat 6.0\webapps\news\test

以上是关于getRequestURI()与getRequestURL()的区别的主要内容,如果未能解决你的问题,请参考以下文章

request.getRequestURL()和request.getRequestURI()

HttpServletRequest 中的 getRequestURI 和 getPathInfo 方法有啥区别?

Tomcat getRequestURI()使用不当绕过鉴权

getRequestURI,getRequestURL的区别

HttpServletRequest 中 getRequestURL和getRequestURI的区别

request的getServletPath(),getContextPath(),getRequestURI(),getRealPath("/")区别