首页 > 其他 > 详细

Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

时间:2016-01-01 07:22:31      阅读:309      评论:0      收藏:0      [点我收藏+]

1 区别

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

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

1.1 System.out.println(request.getContextPath());

打印结果:/news

1.2 System.out.println(request.getServletPath());

打印结果:/main/list.jsp

1.3 System.out.println(request.getRequestURI());

打印结果:/news/main/list.jsp

1.4 System.out.println(request.getRealPath("/"));

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

 


 

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path

这其实就是 获得应用的根url,比如说你的应用的根路径是 http://localhost:8080,那么你列出的代码就是为basePath赋值为 http://localhost:8080
具体点:
1、request.getScheme() 返回协议的名称 http,和后面的"://" 拼起来就成了 http://
2、request.getServerName() 这是获取你的服务器的名称,如果你的应用部署在本机那么其就返回localhost或者127.0.0.1 ,这2个是等价的
3、request.getServerPort() 是你应用使用的端口,比如8080或者80 等等

 

Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别

原文:http://www.cnblogs.com/qingfengzhuimeng/p/5092936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!