打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
getContextPath和getRealPath的区别

这个问题很简单,实验一下就出来了,其实主要区别就是相对路径和绝对路径:

  • getContextPath返回的是相对路径,工程的项目的相对路径;
  • getRealPath返回的绝对路径,就是在文件系统的实际路径;
下面是我自己做的实验,在SpringMvc+Spring+Hibernate的项目中做的实验
  1. @Controller  
  2. public class IndexController {  
  3.   
  4.     @RequestMapping("/")  
  5.     public String index(HttpServletRequest request){  
  6.         System.out.println(request.getContextPath());  
  7.         System.out.println(request.getSession().getServletContext().getContextPath());  
  8.         System.out.println(request.getServletContext().getContextPath());  
  9.         System.out.println(request.getServletContext().getRealPath("/"));  
  10.         System.out.println(request.getSession().getServletContext().getContextPath());  
  11.         System.out.println(request.getSession().getServletContext().getRealPath("/"));  
  12.         return "index";  
  13.     }  
  14. }  

上面的打印结果如下:
  1. /cn.test  
  2. /cn.test  
  3. /cn.test  
  4. E:\StudyResource\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\cn.test\  
  5. /cn.test  
  6. E:\StudyResource\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\cn.test\  
其实request、session、ServletContext调用getContextPath返回的结果是相同的

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java获取绝对路径的问题
request.getSession().getServletContext().getRealPath(“/”);
getRequestURI,getRequestURL的区别
Java 中各种路径的获取 -- lsl小结 -- servlet & JSP 部分
web常用对象2
getRealPath()过期解决方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服